Django doesn't load css

316 views
Skip to first unread message

Andreas Pfrengle

unread,
Jul 28, 2010, 11:21:11 AM7/28/10
to Django users
Hi,

we've been trying for hours now to include a css-file in our project,
but it just doesn't work and we don't know why. We've already reduced
the project to the absolute minimum of necessary code:
----------------------------
#settings.py:
PROJECT_PATH = os.path.abspath(os.path.dirname(__file__))
MEDIA_ROOT = os.path.normpath(os.path.join(PROJECT_PATH,
'site_media/'))
TEMPLATE_DIRS = ('templates', )
<Rest is merely the default settings when starting a new project; the
app is included in ISTALLED_APPS>

#site_media/css/test.css:
body {
background: #9ba49b url("../images/test.jpg"); }

#site_media/images/test.jpg:
<a jpg image>

#templates/base.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="stylesheet" href="../site_media/css/test.css" type="text/
css" media="screen" />
</head>
<body>
test
</body>
</html>

#testapp/views.py:
from django.shortcuts import render_to_response
from django.template import Context, RequestContext

def home(request):
data = {}
return render_to_response('base.html', RequestContext(request,
data))

--------------------
When I drag base.html in the browser, the css is included, loading the
test.jpg image. When loading the page in the django-project, the css
isn't loaded (neither the background color, nor the image). When I
include the css code directly in base.html <style> tag, the background
color is set, but still the image is not loaded (after adjusting the
relative url).
Somehow I feel there's a problem with the path Django tries to access.
However, the method we've defined the PROJECT_PATH and MEDIA_ROOT in
the settings.py already worked in another project. Something else must
be missing, but what?

Thanks in advance for any advice,
Andreas
Message has been deleted

Antoni Aloy

unread,
Jul 28, 2010, 12:35:22 PM7/28/10
to django...@googlegroups.com
the css should be {% MEDIA_URL %} but you have to be sure you pass the
RequestConext on your render_to_response method.
If you're are on the development server you have to add additional
urls to urls.py to serve static content.

Is explained in the docs, but I suppose some examples you'll be
better. I created some startup and example projects that I think they
would help you.

http://code.google.com/p/appfusedjango/

Check project and agenda, they are a good start. The examples are not
updated for 1.2.1 but they work.

2010/7/28 kostia <kostya....@gmail.com>:
> I use in settings.py:
>
> MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'main',
> 'templates', 'static').replace('\\', '/')
>
>
> MEDIA_URL = '/static/'
>
>
> And then simply in an html file:
>
> <link rel="stylesheet" type="text/css" href="{% url static 'styles/
> nav.css' %}" />
>
> Where the full folder path is the ../project_name/main/templates/
> static/styles/nav.css
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>
>

--
Antoni Aloy López
Blog: http://trespams.com
Site: http://apsl.net

Andreas Pfrengle

unread,
Jul 28, 2010, 1:44:15 PM7/28/10
to Django users
Hello Kostia,

I get a template syntax error with your method, since I haven't
defined a "static" view. How does your static-view look like?
I've already tried it like described here --> <http://
docs.djangoproject.com/en/dev/howto/static-files/> but I don't get it
work either, I get a TypeError: bad operand type for unary +: 'list'
for the urlpattern that the docs suggest to include.

Antoni, we've tried it already with the {{ MEDIA_URL }} variable, but
to make it braindead simple we've hardcoded the paths.
On the example you've linked: There is only a picture in the download
section, no code.

Antoni Aloy

unread,
Jul 28, 2010, 2:33:50 PM7/28/10
to django...@googlegroups.com
2010/7/28 Andreas Pfrengle <a.pfr...@gmail.com>:

Use subversion Luke :)

Sorry I can't resist. ;)

Daniel Roseman

unread,
Jul 28, 2010, 2:09:34 PM7/28/10
to Django users
You haven't shown your urls.py, but do you have anything actually
serving from ../site_media/? Have you set up Django to do it (via the
development server), or Apache?
--
DR.
Message has been deleted

Andreas Pfrengle

unread,
Jul 28, 2010, 4:32:51 PM7/28/10
to Django users
Hallo everyone,

thanks for the many answers, the problem is solved. There must have
been an error in the urls.py. Now it works with the static serving:
# from urls.py:
site_media = os.path.join(os.path.dirname(__file__), 'site_media')

urlpatterns += patterns('',
url(r'^site_media/(?P<path>.*)$', 'django.views.static.serve',
{'document_root': site_media}),
)

Now we've also replaced the hardcoded urls with "{{MEDIA_URL}}css/
test.css"
Reply all
Reply to author
Forward
0 new messages