dan*_*ana 17 django indexing program-entry-point set
我想为我的应用程序设置主页面或索引页面.我尝试在settings.py中添加MAIN_PAGE然后创建一个main_page视图返回一个main_page对象,但它不起作用另外,我试图在urls.py中添加一个声明,如
(r'^$', index),
Run Code Online (Sandbox Code Playgroud)
其中index应该是根目录下index.html文件的名称(但显然不起作用)
在Django网站中设置主页的最佳方法是什么?
谢谢!
rya*_*lon 15
这样做的新首选方法是使用TemplateView该类.如果您想要离开,请参阅此SO答案direct_to_template.
在您的主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'),
]
Run Code Online (Sandbox Code Playgroud)
注意,我选择将任何静态页面linke index.html放在目录中自己static_pages/的templates/目录中.
mip*_*adi 13
如果要引用静态页面(不进行任何动态处理),可以使用direct_to_template视图功能django.views.generic.simple.在您的网址中:
from django.views.generic.simple import direct_to_template
urlpatterns += patterns("",
(r"^$", direct_to_template, {"template": "index.html"})
)
Run Code Online (Sandbox Code Playgroud)
(假设index.html是您的一个模板目录的根目录.)
小智 10
如果有人正在寻找答案的更新版本..
from django.urls import re_path
from . import views
urlpatterns = [
re_path(r'^$', views.index, name='index')
]
Run Code Online (Sandbox Code Playgroud)
并在你的views.py
def index(req):
return render(req, 'myApp/index.html')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33724 次 |
| 最近记录: |