我想爲我的應用程序設置主頁面或索引頁面。 我嘗試添加在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