Django 在 '/' url 处提供静态 index.html 视图 [英] Django serve static index.html with view at '/' url

查看:25
本文介绍了Django 在 '/' url 处提供静态 index.html 视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 index.html 在/static/文件夹中.当我尝试时,我的 django 应用程序运行正常:

I have my index.html in /static/ folder. My django app is running ok when i try:

http://127.0.0.1:8000/index.html

但我想通过 url 访问 index.html:

But i want to acces index.html by url:

http://127.0.0.1:8000/

我写了一个视图并且它有效:

I wrote a view and it works:

class IndexView(TemplateView):
    template_name = 'index.html'

我还添加到 urls.py(这让我可以像 http://127.0.0.1:8000/css/style.css 这样的静态服务):

I also added to urls.py(this lets me serve static like http://127.0.0.1:8000/css/style.css):

url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve', {
            'document_root': settings.STATIC_ROOT, 'show_indexes':True
        }),

但我认为有一种方法可以在没有 TemplateView 的情况下做我想做的事情.

But i think there is a way to do what i want without TemplateView.

有什么建议吗?谢谢.我的 Django 版本是:Django 1.5

Any suggestions? Thanks. My django version is: Django 1.5

编辑:

我将 index.html 放入 static 的原因:我想让 Phonegap 兼容 django 应用程序,所以在正确编码后,我所要做的就是 --> 从 static 文件夹中生成 .zip 和将其作为移动应用程序上传到 Phonegap.简单干净.

The reason i placed index.html into static: i want to make Phonegap compatible django app, so after proper coding, all i have to do is --> make .zip from static folder and upload it to Phonegap as mobile app. Easy and clean.

推荐答案

您可以像这样为 static/index.html 开发服务:

You can serve static/index.html for development like this:

if settings.DEBUG:
    urlpatterns += url(
        r'^$', 'django.contrib.staticfiles.views.serve', kwargs={
            'path': 'index.html', 'document_root': settings.STATIC_ROOT}),

但是对于生产,你应该配置你的 nginx(或其他前端服务器)来为 / 位置提供 index.html 文件

But for production you should configure your nginx (or other frontend server) to serve index.html file for / location

更新

我想解释一下你应该这样做的情况.例如,您的 django 应用程序只是管理和 api 视图,但客户端与单页应用程序(Ember、Angular 等)进行交互.所以你的项目至少有两个子项目,一个是你的主 django 应用程序,第二个是一个包含所有 html/js/css 内容的客户端应用程序.将客户端脚本与 django 后端分开非常方便,它允许您的前端开发人员完成他们的工作并避免 django 的存在(总有一天它可以移动到不同的存储库).

I want to explain the case you should do like this. For example your django app is only admin and api view, but client interacts with a single page app (Ember, Angular, whatever). So you project has at least two subprojects, one with your main django app and the second is a client app with all html/js/css stuff. It is very convenient to have client scripts separate from django backend, it allows your frontend developers to do their job and avoid django existence (someday it can be moved to the distinct repo).

因此,在这种情况下,您将获得以下构建工作流程:

So in this case you get the following build workflow:

  1. 运行客户端应用程序源观察器以重建您的脚本/样式/模板(brunch watchgrunt 作业或 gulp watch 任务)
  2. 使用 django 收集静态数据用于生产
  3. 确保您有用于开发的 urlpatterns 修复程序和用于生产的正确 nginx 配置
  1. Run client app sources watcher to rebuild your scripts/styles/templates (brunch watch, grunt job or gulp watch task)
  2. Collect static with django for production
  3. Make sure you have urlpatterns fix for developments and right nginx config for production

这是我的 urls.py 示例

urlpatterns += patterns(
    'django.contrib.staticfiles.views',
    url(r'^(?:index.html)?$', 'serve', kwargs={'path': 'index.html'}),
    url(r'^(?P<path>(?:js|css|img)/.*)$', 'serve'),
)

这篇关于Django 在 '/' url 处提供静态 index.html 视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆