[Django]-Django static page?

66👍

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'),

52👍

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'}),

13👍

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

2👍

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.

1👍

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

👤krubo

Leave a comment