简体   繁体   中英

Setting up static files in Django

I have problems with setting up static files in my Django project on development server. I'm using Django-1.6.1 with Python 2.7.5+.

I followed the instrucions from this link: Managing static files (CSS, images)

So, i added django.contrib.staticfiles into INSTALLED_APPS

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'BlogContent',
)

I set up STATIC_URL :

STATIC_URL = '/static/'

And i also modify my urls.py to this:

urlpatterns = [           
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', home_view ),
    url(r'^about/$', about_view )
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

In template i use this tag:

{% load staticfiles %}
<img src="{% static "elo.jpg" %}"/>

And all files are into project_root/static/ and after run server i recieve this:

"GET /static/elo.jpg HTTP/1.1" 404 1625

Do you have any ideas how to solve it ? Thank you in advance for help.

Django doesn't serve files itself; it leaves that job to whichever Web server you choose. so remove + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) in urls.py . Use Apache2 to store your static or media files

https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#serving-files

In your Apache *.conf file(Apache 2.4)

<VirtualHost *:80>
        ServerName example.com
        ServerAdmin example@example.com

        Alias /media/ /home/tu/blog/media/
        Alias /static/ /home/tu/blog/collected_static/

        <Directory /home/tu/blog/media>
                Require all granted
        </Directory>

        <Directory /home/tu/blog/collected_static>
                Require all granted
        </Directory>

        WSGIScriptAlias / /home/tu/blog/blog/wsgi.py

        <Directory /home/tu/blog/blog>
        <Files wsgi.py>
                Require all granted
        </Files>
        </Directory>
</VirtualHost>

If you use Apache 2.2 use

Order allow,deny
Allow from all

instead of

Require all granted

Note: You can run apachectl -v to see your Apache2 version

And here is a spinet of a nginx configuration to serve static files & proxy to the application called APP:

http {
…
    server {
        …
        location /static/ {
            autoindex on;
            alias /usr/share/nginx/html/static/;
        }

        location /APP {
            return 301 /APP/;
        }

        location /APP/ {
            rewrite ^/intranet(.*) /$1 break; 
            proxy_redirect off;
            proxy_pass http://127.0.0.1:8000;
        }
    }
}

Note that I use gunicorn to run my app on localhost:8000 but I connect to it using http://localhost/APP .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM