我有一个 Django 应用程序(我很新,所以我正在尽我最大的努力学习来龙去脉),我希望有一个 url 端点简单地重定向到一个静态html 文件在另一个文件夹 (app) 中。
我的项目文件层次结构如下:
docs/
- html/
- index.html
myapp/
- urls.py
我的 urls.py 看起来像:
from django.conf.urls import patterns, include, url
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^docs/$', RedirectView.as_view(url='/docs/html/index.html')),
)
但是,当我导航到 http://localhost:8000/docs 时,我看到浏览器重定向到 http://localhost:8000/docs/html/index.html,但页面无法访问。
在这样的重定向中,/docs/html/index.html 是否对 myApp 应用程序不可用?
不胜感激。
请您参考如下方法:
NOTE:
direct_to_templatehas been deprecated since Django 1.5. Use TemplateView.as_view instead.
我想你想要的是 Template View ,而不是 RedirectView。你可以这样做:
urls.py
from django.conf.urls import patterns, include, url
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^docs/$', direct_to_template, {
'template': 'index.html'
}),
)
只要确保 index.html 的路径在 TEMPLATE_DIRS 设置中,或者只是将它放在应用程序的 templates 文件夹中(This answer 可能有帮助)。
