Wednesday, December 1, 2010

Re: Loading CSS

On Tue, Nov 30, 2010 at 6:21 PM, octopusgrabbus
<old_road_farm@verizon.net> wrote:
> I am running Django 1.2.3 -- python -c "import django; print
> django.get_version()" I basically need to know what logs to look at to
> fix a css file not loading.
>
> I am trying to load a css file in my base template
>
> <html>
>    <!-- Test Comment -->
>    <head>
>        <title>{% block title %}Town of Arlington Water Department AMR
> System{% endblock %} </title>
>        <link rel="stylesheet" type="text/css" href="/static_media/
> amr.css" />
>   </head>

So you are requesting the file '/static_media/amr.css'

>
> Here's the css file -- you could say it's a "test file".
>
> body{ background-color: gray;}
> p { color: blue; }
> h3{ color: white; }
>
> In urls.py this is inserted into
>
> urlpatterns = patterns('',
>
>    (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
>        {'document_root': '/home/amr/django'}),
>

and you are enabling file serving from a path starting '/site_media/'
(assuming this is your root urlconf).

Is it clear why it isn't working now? 'site_media' != 'static_media'.

In your django dev console, it shows each URL requested, and the HTTP
return code. It will look something like this:

[01/Dec/2010 09:38:47] "GET /static_media/amr.css HTTP/1.1" 404 2572

The first part ('/static_media/') has to match the URL you put in your
urlconf, and the second part ('amr.css') must exist in the folder that
you specify as document_root (or subfolder etc).

This isn't exactly rocket science, just make everything match up, and it works.

BTW, in your template you can use {{ MEDIA_URL }} to get the prefix to
your media - assuming you have correctly set settings.MEDIA_URL, and
have django.core.context_processors.media in
TEMPLATE_CONTEXT_PROCESSORS and use a RequestContext to render with,
which is a few caveats I guess :)

Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment