1

How to write css path on django setting file when django running on apache with mod_wsgi.
I write apache setting file as below.

#httpd.conf
WSGIPythonHome /opt/python3.4
WSGIPythonPath /opt/python3.4/bin/python3
WSGIScriptAlias / /usr/local/django/mysite/mysite/wsgi.py
Alias /static/ /usr/local/django/mysite/mysite/static/

This Alias command is for CSS path on admin site of django.
I copy static files for admin site.
How to use Django with Apache and mod_wsgi

I am using django version 1.7 and leading tutorial for 1.7.
Tutorial 6 is how to use static CSS and image file on django.
My django file tree is as below

mysite
- manage.py
+ mysite
  - settings.py
  + static
  - urls.py
  - wsgi.py
+ polls
  - admin.py
  - models.py
  + templates
    + polls
      - index.html
  - urls.py
  + static
    + polls
      - style.css
  - views.py

mysite/settings.py

PROJECT_ROOT = os.path.normpath(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(PROJECT_ROOT,'static')

style.css is used by polls/templates/polls/views.py
polls/templates/polls/views.py

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />

I accessed localhost.
However, CSS file for polls does not find. (localhost/static/polls/style.css)
If I modify httpd.conf file as below.

WSGIPythonHome /opt/python3.4
WSGIPythonPath /opt/python3.4/bin/python3
WSGIScriptAlias / /usr/local/django/mysite/mysite/wsgi.py
Alias /static/ /usr/local/django/mysite/polls/static/polls/

It can use static file on polls application.
However, in this case, I can not find static file for admin site.
I would like to use static file on administrate site and my applications.
How can I use Static file on apache with mod_wsgi?

2
  • I think you should set STATIC_ROOT to a dir outside your project, adjust httpd.conf accordingly, then run manage.py collectstatic docs.djangoproject.com/en/1.7/ref/contrib/staticfiles/… Commented Jan 17, 2015 at 13:29
  • I execute collect static. It is work. Thank you, Commented Jan 17, 2015 at 14:10

1 Answer 1

2

You should do something like.

Alias /static/ /path/to/your/STATIC_ROOT/directory/

<Directory /path/to/your/STATIC_ROOT/directory>
    Require all granted
</Directory>

in httpd.conf, You should run ./manage.py collectstatic so that all your static files + django admin static files are collected in STATIC_ROOT directory.

then open up your main urls.py and add these lines at the end

from settings import STATIC_ROOT
if conf.DEBUG == True:
    urlpatterns = patterns('',
        url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': STATIC_ROOT}),
    ) + urlpatterns

add these lines to your settings.py

STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'polls/static'),
)

and also go through the docs.

I hope things will work now.

Sign up to request clarification or add additional context in comments.

1 Comment

it's worth noting that django.views.static.serve, while useful, is intended for dev use only, i.e. with the Django runserver... if setting up Apache + mod_wsgi you'd normally want Apache to serve the static files instead, being faster and more secure

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.