如何使用Django的开发服务器提供收集的静态文件? [英] How to serve collected static files with Django's development server?

查看:133
本文介绍了如何使用Django的开发服务器提供收集的静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使Django的开发服务器提供由 python manage.py collectstatic 命令收集的静态文件。现在我失败了。

I am trying to make Django's development server to serve static files that have been collected by the python manage.py collectstatic command. For now I failed.

我的Django设置文件声明了这一点:

My Django settings file declares this:

STATIC_ROOT = os.path.join(WWW_PATH, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(ROOT_PATH, 'front', 'public'),
    os.path.join(ROOT_PATH, 'front', 'dist')
)

这使得 collectstatic 命令从 os.path.join(ROOT_PATH,'front','public' ) os.path.join(ROOT_PATH,'front','dist') STATIC_ROOT ,并且运行良好。

This makes the collectstatic command copying files from os.path.join(ROOT_PATH, 'front', 'public') and os.path.join(ROOT_PATH, 'front', 'dist') to STATIC_ROOT, and it works perfectly.

我假设它还会告诉Django在 STATIC_ROOT 目录,我错了。即使 STATIC_ROOT 目录不存在,Django也可以提供静态文件。但是,如果缺少 os.path.join(ROOT_PATH,'front'),则Django将不再提供静态文件。

I was assuming that it would also tell Django to look for static files into the STATIC_ROOT directory, I was wrong. Even if the STATIC_ROOT directory does not exists, Django is able to serve the static files. But if the os.path.join(ROOT_PATH, 'front') is missing, Django no more serve the static files.

这表明Django继续从source目录而不是从 STATIC_ROOT 提供静态文件。

This shows that Django continues to serve static files from the sources directories and not from STATIC_ROOT.

因此,我想知道是否有一种方法可以指示Django的开发服务器从 STATIC_ROOT 提供静态文件。有提示吗?

So I would like to know if there is a way to instruct Django's development server to serve static files from STATIC_ROOT. Any hint?

编辑:

在@ e4c5的答案之后,我修改了我的根 urls.py 像这样:

After @e4c5 's answer I modified my root urls.py like this:

static_patterns = [
    url(r'^$', TemplateView.as_view(template_name='index.html'))
]

urlpatterns = [
    url(r'^', include(static_patterns)),
    url(r'^admin/', admin.site.urls),
    url(r'^api/resa/', include('reservation.urls')),
    url(r'^api/auth/', include('authentication.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

不幸的是,它没有预期的结果,并且Django在 STATIC_ROOT ,我怀疑它实际上是在 STATIC_ROOT 中查找静态文件。

unfortunately, it does not have the expected result and Django does not find static files in STATIC_ROOT, I doubt that it actually looks for static files in STATIC_ROOT.

这是我可以做的在访问索引页面时在控制台中看到:

here is what I can see in the console when accessing to the index page:

[04/Jun/2017 16:18:05] "GET / HTTP/1.1" 200 1411
[04/Jun/2017 16:18:05] "GET /static/style/index.css HTTP/1.1" 404 1759
[04/Jun/2017 16:18:05] "GET /static/style/react-datetime.css HTTP/1.1" 404 1786
[04/Jun/2017 16:18:05] "GET /static/style/react-big-calendar.css HTTP/1.1" 404 1798
[04/Jun/2017 16:18:05] "GET /static/script/bundle.js HTTP/1.1" 404 1762

以下是内容设置变量中的一个:

here are the content of the settings variables:

>>> from django.conf import settings
>>> settings.WWW_PATH
'/home/tryph/PycharmProjects/resa/www'
>>> settings.STATIC_ROOT
'/home/tryph/PycharmProjects/resa/www/static'
>>> settings.STATIC_URL
'/static/'

这是<$ c的内容$ c> WWW_PATH 目录:

/home/tryph/PycharmProjects/resa/www
└── static
    ├── admin
    │   [...]
    ├── favicon.ico
    ├── index.html
    ├── rest_framework
    │   [...]
    ├── script
    │   └── bundle.js
    └── style
        ├── index.css
        ├── react-big-calendar.css
        └── react-datetime.css    


推荐答案

这是通过 static.serve


除了项目的静态资产外,可能还有其他文件,例如
的便利性,您想让Django在本地
开发中为您服务。 serve()视图可用于提供
为其提供的任何目录。 (此视图未用于生产用途,应仅将
用作开发辅助工具;您应使用真正的前端Web服务器在
的生产中提供这些文件

There may be files other than your project’s static assets that, for convenience, you’d like to have Django serve for you in local development. The serve() view can be used to serve any directory you give it. (This view is not hardened for production use and should be used only as a development aid; you should serve these files in production using a real front-end web server

像这样更改您的urls.py:

Change your urls.py like this:

from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    # ... the rest of your URLconf goes here ...
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

这篇关于如何使用Django的开发服务器提供收集的静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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