A
A
archi19982014-11-06 22:10:16
Django
archi1998, 2014-11-06 22:10:16

Why don't static files work in django?

For some reason, static files are junk, writes that 404 not found

---settings.py----
STATIC_ROOT = "/home/projects/testproject/static"
STATIC_URL = '/static/'
STATICFILES_DIRS = (
  '/home/projects/testproject/static',
)
STATICFILES_FINDERS = (
  'django.contrib.staticfiles.finders.FileSystemFinder',
  'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
MEDIA_ROOT = "home/projects/testproject/media"
MEDIA_URL = "/media/"
--urls.py--
from django.conf.urls import patterns, include, url
from django.contrib import admin
from programms import views
from django.conf import settings
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'testproject.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url('^$', views.main_page),
)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
    # static files (images, css, javascript, etc.)
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.STATIC_ROOT}))
--nginx.conf---
upstream django{
server unix:///home/projects/testproject/testproject.sock;
}
server{
listen 80;
server_name mysite.ru;
charset utf-8;
client_max_body_size 75M;
location /media/{
alias /home/projects/testproject/media;
}
location /static/{
alias /home/projects/testproject/static;
}
location /{
uwsgi_pass django;
include /home/projects/testproject/uwsgi_params;
}
}
------------------------------------------------------------------------------------

collectstatic did too...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Scherbakov, 2014-11-06
@archi1998

If you provide the nginx config, it means that static files do not work in production, and not in the native django server. Try adding a slash to the end of the path:

location /static/ {
    alias /home/projects/testproject/static/;
}

I vaguely remember that this slash matters.
PS Please use the code tag to format the code. It would be much easier to read.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question