2017-07-26 84 views
1

我有應用程序的url和當我去文章報告URL我看到Django錯誤頁 與文章的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

您應該包括TemplateViewurlpatterns一個url(),而不是試圖與路由器進行註冊。

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文件中,請確保在TEMPLATES -> DIRS中添加了report.html的路徑。

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

你的模板DIR是空的......

確保初始化之前,你的結構DIR path

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

以及使用確保您的網址使用它像這樣,名= '報告'

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不知道什麼是名稱參數,我更改爲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

使用相對目錄「模板」不是一個好主意。或者硬編碼絕對路徑,或者像在其他例子中那樣使用'os.path.join()'。 – Alasdair