0

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?

3 Answers 3

2

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.

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

3 Comments

This is pretty much what I needed. I am reading into that and it all makes sense. I am developing only one site though and don't want to switch between the "runserver" command and running apache to test my site. This is just ONE project. Can I just set everything up as if I am deploying? I prefer to use Apache only to test the site.
Yes, you can do it easily by having all your static files in one directory. You can have subdirectories in there, you just have to address them accordingly (e.g. the main directory is /var/www/static and you have directories for js, css and images in there). You can then link to these files in HTML, e.g. with /static/css/base.css. The one thing to do is to tell apache where the location of the static directory is. In your config, add Alias /static /var/www/static and allow access to this directory in the VirtualHost tag as described by Patrick Bassut.
If you want just one way of doing thing for development and for deployment, then look at mod_wsgi express and its Django integration. pypi.python.org/pypi/mod_wsgi
1

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

12 Comments

WSGIScriptAlias / "C:/mysite/mysite/wsgi.py" WSGIPythonPath "C:/mysite" <Directory "C:/mysite/mysite> <Files wsgi.py> Require all granted </Files> </Directory>
Why doesn't the above code already solve the css problem? I already have the whole site root directory included in the httpd.conf file...
@klandshome what part you think would solve the problem? If it is WSGIPythonPath I think it's because that's mainly for importing modules. If you mean the Files tag, that doesn't do much about serving your static files.
the VirtualHost tag is a configuration apache will execute if the ip and port binded match. Usually we use *:80. Which means for every ip used(127.0.0.1, 100.100.100.100, etc) on port 80 the following(inside the VirtualHost tag) configuration will be used. So you can have multiple configurations for different ips and different ports.
well, you don't need to add a virtualHost tag cause apache will add that for you as soon as you enable a new site(which you do with the a2ensite command. You just need to open your /etc/apache2/sites-available/ and edit it. Here's an example httpd.apache.org/docs/2.2/vhosts/examples.html
|
0

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.

1 Comment

forgot the alias part, sorry about that. The directory tag only allows the access. The alias tag kinda route whatever comes to that /static/ path to that directory you specified.

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.