0

On my local PC I can do "python manage.py runserver" and the site runs perfectly, CSS and all. I just deployed the site to a public server and while most things work, CSS (and the images) are not loading into the templates.

I found some other questions with a similar issue, but my code did not appear to suffer from any of the same problems.

Within the Django project settings the same python function is being used to allow the app to see the templates and the static CSS / image files. The templates are being found by the views and are loading without issue.

Both from settings.py:

STATICFILES_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates/css').replace('\\','/'),
    os.path.join(os.path.dirname(__file__), 'content').replace('\\','/'),
)

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)

In the base.html file which the rest of the templates all extend:

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

Directory structure:

|project_root/
|--manage.py
|--project/
| |--settings.py
| |--__init__.py
| |--content/
| | |--header.jpg
| |--templates/
| | |--base.html
| | |--css/
| | | |--style.css

My first thought when the CSS didn't load is that Django couldn't find the style.css file, but since I am using the same "os.path.dirname(file)" technique as with the templates, I am not sure this is the case.

What do I have wrong here?

Edit:

I neglected to mention that both the PC and server are running Python 2.7.5 and Django 1.5.5.

4
  • How did you deploy your project? For static files, are you following the official howto? Commented Dec 15, 2013 at 5:33
  • read this: hasnath.net/blog/… after so much digging I came up with this. Commented Dec 15, 2013 at 9:04
  • What server are you using? Commented Dec 15, 2013 at 9:45
  • @sha256 that link shows how to set it up for development, in practice it's not a good idea for deployment Commented Dec 15, 2013 at 9:47

2 Answers 2

1

You never mentioned it in your post, so I am guessing:

You never ran ./manage.py collectstatic

collectstatic finds all static files (css, images, js) and puts them in a directory (Choose this directory with the django setting STATIC_ROOT) Then you point your webserver to that directory

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

1 Comment

Exactly right. I also needed to adjust my STATIC_URL to be the public folder which Dreamhost automatically serves out.
1

You should deploy the static files using your server and not django.

The official documentation mentiones using collectstatic but unless your static files are messed up it's usually not a requirement. You just need to have some directory containing all your static files. Then you just push it with the server to the same place django will be looking.

Say you're STATIC_URL is '/static/', so you need to add an alias which would map '/static/' to the static directory. For example, using Apache, you should add this line to your http.conf:

Alias /static/ /path/to/mysite.com/static/

That's it! This thing goes true to media files as well, and it'd be wise to remove any serving of static files done by django for the development server (these kinds of urls). Finally, check out the documentation for even more information regarding other types of deployment

Comments

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.