Django静态文件将无法加载 [英] Django static files won't load

查看:140
本文介绍了Django静态文件将无法加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经使用 django.contrib.auth 由两个模板组成: mysite / templates / index.html mysite / templates / registration /login.html 。我在 mysite / static 中具有全局静态内容,我希望能够访问所有应用程序呈现的所有模板。



mysite / templates / index.html 包含< img src ={{STATIC_URL}} pics03.jpg/> 它呈现为static / pics03.jpg,当我访问url localhost:8000 /



mysite / templates / registration / login.html 包含< img src ={{STATIC_URL}} pics03.jpg/> 它也呈现为static / pics03.jpg并且不加载当我访问url localhost:8000 / accounts / login /



在我的urls.py我有:

  urlpatterns = patterns('',
url(r'^ $','mysite.views.home '),#播放index.html模板
url(r'^ accounts / login / $','django.contrib.auth.views.login'),
/ pre>

在我的settings.py中,我有:

  PROJEC T_DIR = os.path.dirname(__ file__)

STATICFILES_DIRS =(
#将字符串放在这里,如/ home / html / static或C:/ www / django / static 。
#总是使用正斜杠,甚至在Windows上。
#不要忘记使用绝对路径,而不是相对路径。
os.path.join(PROJECT_DIR,'static'),


STATICFILES_FINDERS =(
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',


STATIC_URL ='/ static /'

STATIC_ROOT =''

我的印象是Django应该在STATICFILES_DIRS中寻找全球静态内容,但是没有找到login.html的静态内容,即使我将url更改为静态文件夹的绝对路径。任何人都可以看清这个吗?

解决方案

你的问题是你没有听/ static /您的urls.py



如果您通过像Apache或nginx这样的网络服务器来提供应用程序,那么这是正常的,因为Web服务器将处理静态文件本身。



对于开发Django自带一个内置的静态服务器



到urls.py,最后添加

  from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
urlpatterns + = staticfiles_urlpatterns()

这是什么是添加/ static / url,让您无需使用网络服务器。



这相当于

  url(
regex = r'^ static /(?P< path> ; $ * $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ code>

e人们会告诉你,你需要将URL规则包装在一个if settings.DEBUG中以使用dev-only规则,但这根本不需要,实际上我发现这是一个不好的建议。


i'm a Django newbie working on my first project and having a problem with static files.

I have created a simple auth system using django.contrib.auth consisting of two templates: mysite/templates/index.html and mysite/templates/registration/login.html. I have global static content in mysite/static which I want to be able to access on all templates rendered by all apps.

mysite/templates/index.html contains <img src="{{ STATIC_URL }}pics03.jpg"/> which renders as "static/pics03.jpg" and loads fine when I visit the url localhost:8000/

mysite/templates/registration/login.html contains <img src="{{ STATIC_URL }}pics03.jpg"/> which also renders as "static/pics03.jpg" and does not load when I visit the url "localhost:8000/accounts/login/"

In my urls.py I have:

urlpatterns = patterns('',
   url(r'^$', 'mysite.views.home'), # plays index.html template
   url(r'^accounts/login/$', 'django.contrib.auth.views.login'),

In my settings.py I have:

PROJECT_DIR = os.path.dirname(__file__)

STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_DIR,'static'),
)  

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

STATIC_URL = '/static/'

STATIC_ROOT = ''

I was under the impression that Django should be looking for global static content in STATICFILES_DIRS, but it doesn't find the static content for login.html even if I change the url in there to an absolute path to the static folder. Can anyone shed any light on this?

解决方案

Your problem is that you arent listening to the URL "/static/" nowhere in your urls.py

If you serve your application via a webserver like apache or nginx then this is normal as the webserver would handle the static files itself.

For development Django comes with a built-in static server

to urls.py, at the very end add

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()

What this does is to add the /static/ url and let you serve those without a webserver.

This is equivalent to

url(
    regex=r'^static/(?P<path>.*)$', 
    view='django.views.static.serve', 
    kwargs={'document_root': settings.STATIC_ROOT,}
)

some people will tell you that you need to wrap the URL-rules in a "if settings.DEBUG" to use the dev-only rules, but this isnt needed at all and actually i find that to be a bad advice.

这篇关于Django静态文件将无法加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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