Snerd
Snerd

Reputation: 1553

css not loading with apache but works with django runserver command

So I did the entire django tutorial for version 1.6 and installed Apache 2.4 to test it. So I use this command: python manage.py runserver and then go to the default address for the admin page: http://127.0.0.1:8000/admin/ It is nice and centered and styled.

Now when I close the server and run Apache and go to the same link, it is not styled. So I am assuming CSS is not working. I looked at the Apache logs and don't see any permission errors.

What did I miss?

Upvotes: 0

Views: 3101

Answers (3)

Snerd
Snerd

Reputation: 1553

Figured it all out.

Alias /static/ "C:/mysite/polls/static/"
<Directory "C:/mysite/polls/static">
Require all granted
</Directory>

This is strictly in regards to WINDOWS. The info regarding virtualhosts does not apply. I simply added the above code to my httpd.conf (the config to Apache in Windows) and it now loads the css. The Alias directive was required. I did not have to edit the vhosts file at all and it is running the defaults since I setup Apache 2.4.

Windows is very different than the traditional unix setup (files and locations differ...). Django doc should have a Windows section as well but this page answers everything:

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

Thanks to all.

Upvotes: 0

Patrick Bassut
Patrick Bassut

Reputation: 3338

The reason for this is the way apache handles a filesystem is different from the way django builtin-server does. So, you must tell apache that every urls that begins with /static/ will not be passed out to wsgi(which is the module that handle python requests on apache). Instead it will be redirected to a "physical" filesystem.

Consider that the following instructions are based on a ubuntu environment.

Go to your /var/apache2/sites-available/site-your-using.conf(likely 000-default.conf) and add this inside your VirtualHost tag:

<Directory /path/to/your/static_files>
    Order deny,allow
    Allow from all
</Directory>

do a sudo service apache2 restart and you're ready to go

Upvotes: 1

Tim Zimmermann
Tim Zimmermann

Reputation: 6420

Have a look at https://docs.djangoproject.com/en/dev/howto/static-files/#deployment.

During development, runserver is looking for static files in some pre- and user-specified directories. However, apache is not able to do that, because it does not know these directiories. With collectstatic, django offers a functionality to copy all necessary static files to a single location, which then has to be specified in the config of apache.

Upvotes: 2

Related Questions