简体   繁体   中英

Django Class-based View and URL configuration

I am trying to learn Django and I am stuck with Class-based views and URLconf. From what I understand I should see my test.html at localhost/app1/test1/test/test.html , but I get an error page not found. I am not sure what I am doing wrong.

#~/project_folder/mysite/ulrs.py
urlpatterns = [
path('admin/', admin.site.urls),
path('app1/test1/', include("app1.urls")),
]

#~/project_folder/app1/urls.py
urlpatterns = [
path('test/', TemplateView.as_view(template_name="test.html"), name="home"),
]

#~/project_folder/static/templates
test.html

#~/project_folder/mysite/settings.py
TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        os.path.join(BASE_DIR, 'static/templates'),
    ],
    'APP_DIRS': True,
...
}

STATIC_URL = os.path.join(BASE_DIR, 'static/')
  • Django html-templates are not supposed to be treated as static files and regular path for them is like "app/templates/app" (examine tutorial and demo-projects)
  • template file names are not exposed to the end-user and do not partake in url path, so this particular setup

    path('app1/test1/', include("app1.urls")), path('test/', TemplateView.as_view(template_name="test.html"), name="home"),

actually means: for rendering response on url app1/test1/test/ request use test.html template. If you rename template filename to "whatever-foo-bar-my-template.html" url will not change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM