Django static page?

后端 未结 5 1503
迷失自我
迷失自我 2021-01-30 11:25

I want to make a static page which will be shown to the user only if he/she clicks on a link provided in one of my models. I can do this by making a Python page alone and callin

相关标签:
5条回答
  • 2021-01-30 11:34

    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.

    0 讨论(0)
  • 2021-01-30 11:42

    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'),
    
    0 讨论(0)
  • 2021-01-30 11:49

    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

    0 讨论(0)
  • 2021-01-30 11:50

    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'}),
    
    0 讨论(0)
  • 2021-01-30 11:56

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

    0 讨论(0)
提交回复
热议问题