Django在“ /”网址中提供带有视图的静态index.html [英] Django serve static index.html with view at '/' url

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

问题描述

我在/ static /文件夹中有我的index.html。当我尝试时,我的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

EDIT

我将index.html放入静态的原因:我想制作与Phonegap兼容的django应用程序,因此在正确编码后,我要做的就是-> 从静态制作.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应用仅是admin和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. 运行客户端应用程序源监视程序以重建脚本/样式/模板( 早午餐观看咕unt声工作或 gulp 监视任务)

  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在“ /”网址中提供带有视图的静态index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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