Django runserver不提供开发中的静态文件 [英] Django runserver not serving static files in development

查看:501
本文介绍了Django runserver不提供开发中的静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django与 runserver 进行开发。当我部署到我的生产服务器时,我可以看到我所有的静态文件,但不能在本地计算机上。



我做了 collectstatic 我已经设置了 DEBUG = True



我在网上发现了许多不同的意见,最突出的 STATICFILES_DIRS ,但这对我来说不起作用。



如何设置它,以便在我的开发环境中我可以看到静态文件,当我将文件上传到服务器时,我不需要对生产环境进行任何更改才能正常工作。



编辑 - 我的 urls.py 文件:

 从django.conf.urls导入模式,include,url 
from django.conf.urls.static import static
import newsflashes
import settings

from django.contrib import admin
admin。 autodiscover()

urlpatterns = patterns('',
url(r'^ admin / doc /',include('django.contrib.admindocs.urls')),
url(r ^ admin /',include(admin.site.urls)),
url(r'^',include('newsflashes.urls')),
)+ static(settings.STATIC_URL,document_root = settings.STATIC_ROOT)

编辑 - 文件结构:



我有两个静态和动态的目录。内部静态是静态文件,在动态目录中是django应用程序。



编辑 - 设置:



我的相关设置如下

  STATIC_ROOT = os.path.join(BASE_DIR,'..','static') 
STATIC_URL ='/ static /'
STATICFILES_DIRS =()

谢谢

解决方案

我设法修复了。



我创建了另一个名为静态在我的项目文件夹中称为静态,并复制了我的静态文件。



然后我添加:

  from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
import settings
if settings.DEBUG == True:
urlpatterns + = staticfiles_urlpatterns()

到我的 urls.py



  STATICFILES_DIRS =(os.path.join(BASE_DIR,'static')) 

到我的 settings.py



然后,当我部署我执行 manage.py收集静态并且由于Apache配置正确,一切都会奏效!



根据 http://dlo.me/archives/2013/01/14/how-to-serve-static-files-django/



感谢所有。


I am using Django with runserver for my development. When I deploy to my production server I can see all my static files, but not on my local computer.

I did collectstatic and I have set DEBUG = True.

I found many different opinions online, the most prominent being the STATICFILES_DIRS, but that does not work for me.

How can I set it so that in my development environment I can see the static files, and when I upload my files to the server I do not need to make any changes for the production environment to work properly.

Edit - my urls.py file:

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
import newsflashes
import settings

from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^', include('newsflashes.urls')),
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Edit - file structure:

I have two directories, static and dynamic. Inside static are the static files, and in the dynamic directory are the django apps.

Edit - Settings:

My relevant settings are as follows

STATIC_ROOT = os.path.join(BASE_DIR, '..', 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = ()

Thanks

解决方案

I managed to fix it.

I created another directory called static in my project folder called static, and copied there my static files.

I then added:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
import settings
if settings.DEBUG == True:
    urlpatterns += staticfiles_urlpatterns()

to my urls.py

and

STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)

to my settings.py.

Then, when I deploy I execute manage.py collectstatic and since Apache is configured properly, everything will work!

Based on http://dlo.me/archives/2013/01/14/how-to-serve-static-files-django/

Thanks to all.

这篇关于Django runserver不提供开发中的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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