我想为我的应用程序设置主页面或索引页面。 我尝试添加在settings.py MAIN_PAGE然后创建返回main_page对象main_page看法,但它不工作 此外,我试图在urls.py添加像Django如何设置主页
(r'^$', index),
其中indexshould声明是根上index.html文件的名称(但它显然不起作用)
什么是在Django网站中设置主页的最佳方式是什么?
谢谢!
我想为我的应用程序设置主页面或索引页面。 我尝试添加在settings.py MAIN_PAGE然后创建返回main_page对象main_page看法,但它不工作 此外,我试图在urls.py添加像Django如何设置主页
(r'^$', index),
其中indexshould声明是根上index.html文件的名称(但它显然不起作用)
什么是在Django网站中设置主页的最佳方式是什么?
谢谢!
如果你想引用一个静态页面(没有它经过任何动态处理),你可以从django.views.generic.simple
使用direct_to_template
视图功能。在您的网址的conf:
from django.views.generic.simple import direct_to_template
urlpatterns += patterns("",
(r"^$", direct_to_template, {"template": "index.html"})
)
(假设index.html
是在你的模板目录之一的根。)
你可以使用通用direct_to_template
视图功能:
# in your urls.py ...
...
url(r'^faq/$',
'django.views.generic.simple.direct_to_template',
{ 'template': 'faq.html' }, name='faq'),
...
非常感谢! – dana 2010-07-08 13:45:21
这样做将是使用TemplateView
类的新首选方式。如果您想从direct_to_template
转移,请参见SO answer。
在你的主urls.py
文件:
from django.conf.urls import url
from django.contrib import admin
from django.views.generic.base import TemplateView
urlpatterns = [
url(r'^admin/', admin.site.urls),
# the regex ^$ matches empty
url(r'^$', TemplateView.as_view(template_name='static_pages/index.html'),
name='home'),
]
注意,我选择把临客index.html
任何静态页面在自己的目录static_pages/
的templates/
目录中。
此解决方案非常完美! – Deadpool 2017-04-19 14:14:47
此解决方案已被弃用。请参阅http://stackoverflow.com/questions/11428427/no-module-named-simple-error-in-django上的选定答案。 – 2017-01-13 20:22:26