Python Question

Django 404 error with TemplateView

I have urls in app and when I go to article-report url I see django error page
with urls for article, but there is no url for article-report.

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


part of 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',
],
},
},
]


The terminal receives a message

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


P.S.
Pycharm underlines django,TemplateView and rest_framework in import lines.
But in other examples It doesnt matter

Answer Source

You should include the TemplateView as a url() in urlpatterns, instead of trying to register it with the router.

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download