Python Django urls 直接到 html 模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3402708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django urls straight to html template
提问by Derek Organ
Learning django & python.
学习django & python。
Just set up a new site after doing the tutorial. Now for arguments sake say I want to add a bunch of About us, FAQ basic html pages with very limited dynamic elements do you go ahead and write a new line in my urls.py file for each page? or is their some neat way to say map all * *.html to the relevant .html file directly?
完成教程后只需设置一个新站点。现在为了争论,说我想添加一堆关于我们,常见问题解答基本的 html 页面,动态元素非常有限,你继续在我的 urls.py 文件中为每个页面写一个新行吗?或者他们有什么巧妙的方式可以直接将所有 * *.html 映射到相关的 .html 文件?
In general even if it does require a view will I have to write a new line in the url.py file for every page?
一般来说,即使它确实需要一个视图,我是否必须在 url.py 文件中为每个页面写一个新行?
采纳答案by Peter Shinners
As long as there is some uniquely identifying section in the URL, you will not need to create an entry in urls.py for each direct-template url.
只要 URL 中有一些唯一标识部分,您就不需要在 urls.py 中为每个直接模板 url 创建条目。
For example, you could say that all urls ending in ".html" are referencing a direct file from the templates.
例如,您可以说所有以“.html”结尾的 url 都引用了模板中的直接文件。
urlpatterns = patterns('django.views.generic.simple',
(r'(.+\.html)$', 'direct_to_template'),
# ...
)
Take a look at http://docs.djangoproject.com/en/1.2/ref/generic-views/#django-views-generic-simple-direct-to-templatefor details.
回答by Manoj Govindan
One way to do this would be to write a single custom view that wraps the direct_to_templategeneric view. The wrapper could accept a parameter and accordingly form the name of the template and pass it to direct_to_template. This way you can route multiple pages with a single URL configuration.
一种方法是编写一个包含direct_to_template通用视图的自定义视图。包装器可以接受一个参数并相应地形成模板的名称并将其传递给direct_to_template. 通过这种方式,您可以使用单个 URL 配置路由多个页面。
Something like this:
像这样的东西:
url(r'^foo/(?P<page_name>\w+).html$', 'my_static_wrapper', name = 'my_static_wrapper'),
def my_static_wrapper(request, page_name):
# form template name and call direct_to_template
That said I suspect that there are better solutions out there though.
也就是说,我怀疑那里有更好的解决方案。
回答by brianz
Write a url which grabs the static pages you're interested in
编写一个 url 来抓取您感兴趣的静态页面
url(r'^(?P<page_name>about|faq|press|whatever)/$', 'myapp.staticpage', name='static-pages')
The staticpageview function in myapp
在staticpage考虑到功能myapp
from django.views.generic.simple import direct_to_template
from django.http import Http404
def staticpage(request, page_name):
# Use some exception handling, just to be safe
try:
return direct_to_template(request, '%s.html' % (page_name, ))
except TemplateDoesNotExist:
raise Http404
Of course, you need to follow a naming convention for your templates, but this pattern can be expanded upon as needed.
当然,您需要遵循模板的命名约定,但可以根据需要扩展此模式。
This is better than the .+\.htmlpattern because it will treat templates which don't exist as 404s, whereas .+\.htmlwill blow up with 500 errors if the template doesn't exist.
这比.+\.html模式更好,因为它会将不存在的模板视为 404,而.+\.html如果模板不存在,则会出现 500 个错误。
回答by kirpit
If you're using the class based viewsbecause direct_to_templatehas been deprecated, you can create a simple wrapper that renders your own templates directly:
如果您使用基于类的视图,因为direct_to_template已被弃用,您可以创建一个简单的包装器来直接呈现您自己的模板:
from django.views.generic import TemplateView
from django.template import TemplateDoesNotExist
from django.http import Http404
class StaticView(TemplateView):
def get(self, request, page, *args, **kwargs):
self.template_name = page
response = super(StaticView, self).get(request, *args, **kwargs)
try:
return response.render()
except TemplateDoesNotExist:
raise Http404()
and in your router:
并在您的路由器中:
from myapp.static.views import StaticView
urlpatterns = patterns('',
url(r'^(?P<page>.+\.html)$', StaticView.as_view()),
# ...
)
回答by Igor Pomaranskiy
Currently the best way to do this is using TemplateView from generic class-based views:
目前最好的方法是从通用的基于类的视图中使用 TemplateView:
from django.views.generic.base import TemplateView
url(r'^$', TemplateView.as_view(template_name='index.html'), name='home'),

