如何直接转到Django的urls.py中的模板?


Answers:


261

Django 2.0以上

使用基于类的通用视图,但要使用django 2.0+模式进行注册。

from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
    path('foo/', TemplateView.as_view(template_name='foo.html'))
]

https://docs.djangoproject.com/zh-CN/2.0/ref/class-based-views/base/#templateview

Django 1.5以上

使用基于类的通用视图。

from django.views.generic import TemplateView

urlpatterns = patterns('',
    (r'^foo/$', TemplateView.as_view(template_name='foo.html')),
)

Django <= 1.4

文档:https : //docs.djangoproject.com/zh-CN/1.4/ref/generic-views/#django-views-generic-simple-direct-to-template

urlpatterns = patterns('django.views.generic.simple',
    (r'^foo/$',             'direct_to_template', {'template': 'foo_index.html'}),
    (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}),
)

对于1.5+,我们是否需要对每个静态HTML都执行此操作?
Anupam

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.