Django static files won't load

2020-02-08 08:14发布

问题:

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?

回答1:

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.



回答2:

Are you having trouble when using the build in runserver or are you serving using Apache or similar? I've struggled with this a bit. The documentation I follow is: https://docs.djangoproject.com/en/dev/howto/static-files/

The second part is key when you are ready to deploy. You need to define a static root (which will be empty to begin with) and run the manage.py collectstatic command to move the static files from throughout your project into that folder. Then you can serve them from there.



回答3:

Does changing STATIC_ROOT='' to STATIC_ROOT='/' help?

It seems to me the only difference is that static/pics03.jpg (relative path) exists on the home page, but doesn't on the other.

The absolute path /static/pics03.jpg exists in both cases. If changing STATIC_ROOT doesn't help, just add a / to the beginning of the urls.