2017-07-26 13 views
1

私はアプリでurlを持っていますが、article-report urlに行くと、djangoのエラーページ に記事のURLがありますが、article-reportのURLはありません。Django 404のTemplateViewでのエラー

main.urls.py

from rest_framework import routers 
from main.views import (ArticleViewSet) 
from django.views.generic import TemplateView 

router = routers.DefaultRouter() 
router.register(r'article-report', TemplateView.as_view(template_name = 'report.html'),'report') 
router.register(r'article', ArticleViewSet) 
urlpatterns = router.urls 

settings.py

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

の一部の端子は、メッセージ

[26/Jul/2017 14:22:14] "GET /article-report HTTP/1.1" 404 5777 

P.S.を受信します Pycharmはインポート行にdjango、TemplateView、rest_frameworkの下線を引いています。 他の例ではそれは問題ではありません

+0

あなたの設定を共有 'TEMPLATES'変数が存在しますファイル。その構成を共有します。 –

+0

私は 'TEMPLATES'設定の答えを追加しました。これをチェックしてください。 –

答えて

3

代わりに、ルータに登録しようとしているの、urlpatternsurl()としてTemplateViewを含める必要があります。あなたのテンプレートDIRが空だった

router = routers.DefaultRouter() 
router.register(r'article', ArticleViewSet) 

urlpatterns = [ 
    url(r'article-report', TemplateView.as_view(template_name='report.html'), name='report') # note kwarg name='report' instead of arg 'report' 
] 

urlpatterns += router.urls 
1

settings.pyファイルで、report.htmlのパスがTEMPLATES -> DIRSに追加されていることを確認してください。

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [ 
      os.path.join(BASE_DIR, 'quiz.templates') 
      # your path instead of this^
     ], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 
+0

'DIRS':[os.path.join(BASE_DIR、 'main/templates')]この行を追加しましたが、結果はありません –

+0

あなたはまたルータの問題を解決する必要があります。あなたのコードには両方の問題がありました。 @Alasdairはどのように説明しています。彼の答えをチェックしてください。 –

1

...

のinit前に、あなたの構造を確保DIR path

my_app-> 
    my_app 
    static 
    templates-> 
     report.html 
    manage.py 

だけでなく、あなたのURLは、この、名= 'レポート'のようにそれを使用し確実に使用

urls.py

url(r'article_report', TemplateView.as_view(template_name = 'report.html'), name='report') 

settings.py

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [ 
      os.path.join(BASE_DIR, 'your_app.templates') 
     ], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'debug': DEBUG, 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'production_app.context.bot_context_processor' 
      ], 
     }, 
    }, 
] 
+0

my_appは他のアプリの中にあります。私はこのモジュールのsettings.pyを追加しようとしましたが、結果はありません。 Djangoは名前paramが何であるか分からず、私はbase_nameに変更して結果を出しません。 –

+0

あなたのapp.templatesを定義します... –

-1
. 
├── djangooo 
│   ├── __init__.py 
│   ├── settings.py 
│   ├── urls.py 
│   └── wsgi.py 
├── manage.py 
└── templates 
    └── report.html 




TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [ 
      'templates', # if you use templates folder, you add this. 
     ], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 
+0

ディレクトリはどのように基本パスを取るでしょうか? –

+0

djangoはベースパスを知っています。 –

+0

相対ディレクトリ '' templates ''を使うのは良い考えではありません。絶対パスをハードコードするか、他の例のように 'os.path.join()'を使用してください。 – Alasdair

関連する問題