Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django site in developement: CSS not loading for all pages

I'm afraid I'm at a loss here, I checked out some similar questions but none of them seem to apply.

I am running the django developement server on my laptop. I use it to serve static files as well. I have a folder in the static root which contains the CSS for 2 templates both called base.html.

this is the head section of the one that works:

<head>
    <title>| Entries | Latest entries</title>
    <link rel="stylesheet" type="text/css" href="/static/css/serenity.css" />
</head>

here is the one which does not:

<head>
    <title>Latest Photo Galleries</title>
    <link rel="stylesheet" type="text/css" href="/static/css/photologue.css" />
</head>

urls.py:

    (r'^static/(?P<path>.*)$', 'django.views.static.serve',{'document_root': settings.STATIC_ROOT}),

and the static URL:

    STATIC_URL = '/static/'

The STATIC_ROOT is an absolute path.

when I look at the source and try to open the link to the css I get a 404(not found)

why does Django serve the css file for one app but not the other? (the one not working is in the photologue template.)

Any help would be appreciated.

like image 419
Maarten Avatar asked Nov 22 '25 10:11

Maarten


2 Answers

The way you are using staticfiles is slightly incorrect. While I can't tell you exactly what is causing your current situation, I can tell you that your method will cause you headaches in the future. First things first though, I agree with the comments about watching your request traffic in the Django server terminal. Look for 4xx responses and make sure the requested URL is correct. This: /Static/css/photologue/css has two errors in it.

If you don't want to read further, drop the urls.py static.server line and watch the server terminal. Now, here's how it's all working...

You've got your settings variables correct but you may misunderstand the purpose of STATIC_ROOT. STATIC_URL is the fully qualified or relative URL for your static files. STATIC_ROOT is the folder that will ultimately hold all the static files. It should be empty. Django is responsible for filling it via the manage.py collectstatic command. The idea is each app in your Django project has its own static/ folder with the js/css/image assets that it needs. In addition, Django will collect the static assets for the Admin and any other third-party packages you use. All of these assets will be organized into your STATIC_ROOT folder. It's not safe to assume files you have their prior to collection will remain.

STATIC_ROOT = '/path/to/empty/static/folder/'  # or something dynamic with os.path methods
STATIC_URL = '/static/'

In your case maybe your serenity app has serenity/static/css/serenity.css and photologue has photologue/static/css/photologue.css. You could put shared assets in a base/static/ folder.

Now for properly serving static media. Do not use the 'django.views.static.serve' line in urls.py. Django's runserver will automatically serve static files. Behind the scenes it is handling the collectstatic behavior and gathering all your static assets together and serving them up. Using that type of URL pattern in Django 1.3 is unnecessary, a source of confusion, and the kind of thing that will mistakenly go to production. Remember, your webserver (Apache/Nginx) serves static assets. Your Django urls.py files don't need to know a thing about 'em.

Referring to the static URL in templates. You've got /static/ hardcoded in your templates. That will work (but only because STATIC_URL is the same value). To be more flexible about it, you've got three options.

  1. Use href="{{ STATIC_URL }}css/photologue.css". That variable will be in your templates as long as you include 'django.core.context_processors.static' in your TEMPLATE_CONTEXT_PROCESSORS.
  2. Use a templatetag: {% load static %} ... href="{% get_static_prefix %}css/photologue.css"
  3. In Django 1.4 you'll be able to use {% load static from staticfiles %}... href="{% static 'css/photologue.css' %}"

It's worth reading up on static files in Django and being aware of the changes coming in Django 1.4

like image 146
JCotton Avatar answered Nov 24 '25 00:11

JCotton


I see two issues that are likely to happen:

  • The STATIC variables were not set in your settings file.

(you had lots of answers for this)

  • You have probably not set the /static/ alias in your apache config.

Add this line to your /etc/apache2/httpd.conf file or /etc/apache2/sites-available/myDjangoConfig:

Alias /static/ {valueOfSTATIC_ROOT}

Then restart your Apache server:

sudo service apache2 reload
like image 43
Lilley Avatar answered Nov 24 '25 01:11

Lilley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!