0

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!

Robin
  • 5,366
  • 17
  • 57
  • 87

2 Answers2

2

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 
laurentm
  • 33
  • 6
  • Ok I tried the way you told me. But I think its still not serving the the static. Should I point my html file to load from `static_files_for_facebook/css` files? – Robin Aug 22 '13 at 17:11
  • Are you using the [static template tag](https://docs.djangoproject.com/en/1.5/ref/contrib/staticfiles/#template-tags) in your templates? Can you give an URL example to one of your static file? – laurentm Aug 22 '13 at 17:29
  • I am using this - `{% load static %}`, if this is what you mean. I am a newbie, I don't have much knowledge. Or should I use `{% load static from staticfiles %}`? Or are you referring to something else? This is one of the url if it helps: `url(r'^face/$', 'facebook.views.home'),` – Robin Aug 22 '13 at 17:44
  • Yes, you should use `{% load static from staticfiles %}`. If your static files can't be found, you should have some 404 errors in your apache log files. Check the path that Apache is trying to open, then adapt either your apache configuration or `STATIC_URL`. – laurentm Aug 22 '13 at 17:50
  • I have added `{% load static from staticfiles %}` to the html file, and also added the html file and settings.py here in the forum, but its still now showing up in the `localhost`. – Robin Aug 22 '13 at 17:56
  • In my html file I have link the css with 'assets', `href="{% static "assets/css/face.css" %}">`. But there are two folders `assets` and `admin`, after I made the command collectstatic. Is it the problem? Should I direct css to `admin`? – Robin Aug 22 '13 at 18:14
0

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.

Robin
  • 5,366
  • 17
  • 57
  • 87