Django-重定向到静态html fi

2024-04-29 14:03:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Django应用程序(我是一个相当新的应用程序,所以我正在尽我最大的努力学习输入和输出),我想让一个url端点简单地重定向到另一个文件夹(应用程序)中的一个静态html文件。

我的项目文件层次结构如下:

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应用程序?

一个指针将非常感谢。


Tags: 文件djangofrompyimport应用程序httpurl
2条回答

NOTE: direct_to_template has been deprecated since Django 1.5. Use TemplateView.as_view instead.

我想你想要的是一个Template View,而不是一个重定向视图。你可以这样做:

url.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可能有帮助)。

我敢肯定Django正在寻找一个与/docs/html/index.html匹配的URL路由,它不知道服务一个静态文件,当它找不到路由时,它显示一个错误

相关问题 更多 >