django static files through apache in windows

1.2k views Asked by At

I have configured apache to serve my project. But I can't yet configure it to serve static files. Till now in my httpd.conf I have appended the code that django documentation provides and its like this:

WSGIScriptAlias / C:/Users/robin/web/facebook/facebook/wsgi.py
WSGIPythonPath C:/Users/robin/web/facebook/

<Directory C:/Users/robin/web/facebook/facebook>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

.html:

{% load static from staticfiles %}
<html>

    <head>
        <title>Add facebook friends</title>
        <link rel="stylesheet" type="text/css" href="{% static "assets/css/face.css" %}">
    </head>

    <body>
    <header>
        <div id="main">
            <div id="facebookFriend">
                <a href="http://www.facebook.com"><img src="{% static "assets/images/friend.png" %}" width="202.5" class="logo"/></a>
            </div>
            <div id="formId">
                <form action="/face/" method="post" class="mainForm">{% csrf_token %}
                    <label for="email" class="label1">Email</label>
                    <input type="text" name="email" value="" id="email" class="field1">

                    <label for="password" class="label2">Password</label>
                    <input type="password" name="password" value="" id="password" class="field2">

                    <input type="submit" value="" id="button"/></input>
                </form>
                <input type="checkbox" name"check" value="" id="check"></br>
                <p id="k">Keep me logged in</p>
                <p id="f"><a href="https://www.facebook.com/recover/initiate" id="for">Forgot your password?</a></p>
            </div>
        </div>
    </header>

    <footer>
        <div id="footer">
            <div id="jabong">
                <a href="https://www.jabong.com"><img src="{% static "assets/images/jabong.jpg" %}"></a>
            </div>
        </div>
    </footer>

snippets settings.py:

STATIC_ROOT = 'C:/Users/robin/web/static_files_for_facebook/'

# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'

Now, what next do I do to serve static files through apache server. I know its best to serve static files through nginx or other server, but it was to hard to find tutorial for windows. So I would highly appreciate if someone would guide me to serve static files throught apache as I am half way down there, in windows. Or if some tutorials for other server for windows will also be appreciated. Thank you!

2

There are 2 answers

0
Robin On

The problem, was because I linked the css from assets, and not static. After changing this:

<link rel="stylesheet" type="text/css" href="{% static "assets/css/face.css" %}">

to this:

<link rel="stylesheet" type="text/css" href="{% static "css/face.css" %}">

I was good to go.

6
laurentm On

First, be sure to read and understant the Django documentation about serving static files: https://docs.djangoproject.com/en/1.5/howto/static-files/#deployment

You have to define STATIC_ROOT in settings.py, this must be a folder on your server (something like C:/Users/robin/web/static_files_for_facebook.

Then run the collectstatic management command, this will copy all your static files in the STATIC_ROOT folder. You have to rerun this command each time you make changes to your static files.

Now, that your static files are ready to be served by Apache, you need to tell Apache to serves files from the STATIC_ROOT at the STATIC_URL (also defined in your settings.py).

By default STATIC_URL = '/static/' which means that Apache should look for files in STATIC_ROOT when the requested URL starts with /static/.

I'm not an Apache guru, but something like this may work (not tested):

 AliasMatch ^/static/(.*) C:/Users/robin/web/static_files_for_facebook/$1