Django static page?

做~自己de王妃 提交于 2019-12-02 15:36:58
David Grellscheid

With the class-based views in newer Django versions, one can use this in urls.py:

from django.views.generic import TemplateView
url(r'^about', 
    TemplateView.as_view(template_name='path/to/about_us.html'),
    name='about'),

Bypassing views to render a static template, add this line in "urls.py". For example "About Us" page could be

(r'^about', 'django.views.generic.simple.direct_to_template', {'template': 'path/to/about_us.html'}),

Do you mean something like Django's flatpages app? It does exactly what you describe.

If you want to make a static page the flatpages is a good choice. It allows you to easily create static content. Creating static content is not harder than creating a view really.

On Django 2.2.6, loosely following David's answer, I added the path in urls.py:

from django.views.generic import TemplateView

urlpatterns = [
    .... .... ....
    path('about',
         TemplateView.as_view(template_name='path/to/about_us.html'),
         name='about'),

And I needed to adjust settings.py to specify the template directory:

TEMPLATES = [{
    ... ... ...
        'DIRS': [os.path.join(BASE_DIR, 'template')],

Then I saved the actual content in template/path/to/about_us.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!