Django static page?

99封情书 提交于 2019-12-03 01:59:47

问题


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 calling it, but I want it be called from Django. The user interface should be constructed using the Django API only.

Any suggestions?


回答1:


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



回答2:


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



回答3:


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




回答4:


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.




回答5:


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



来源:https://stackoverflow.com/questions/1123898/django-static-page

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