Django 1.3 Static Files - How?

54 views
Skip to first unread message

towerofbabel

unread,
Jun 6, 2011, 4:49:40 AM6/6/11
to pywe...@googlegroups.com
Perhaps someone knows how staticfiles works?


I am developing a site on Django 1.3, and I am basically a beginner at this. My development environment is actually on Apache via wsgi.

I am trying to use the new staticfiles feature of 1.3

I have defined a static directory and a static URL in settings.py. 

The static URL is configured in Apache to point to the static directory - this is confirmed as working.

I have confirmed that my static directory is available to django by running mange.py collectstatic - this writes my admin static files, and other static files I have in a a separate directory, to the declared static directory.

In my template, I refer to static files like this:

href="{{ STATIC_URL }}css/some.css"

The problem is that when the HTML is rendered, I get this:

href="css/some.css"


I don't understand why django doesn't populate {{ STATIC_URL }} with the URL i have defined in settings.py

Has anyone had a similar problem?



There is no static URL.








towerofbabel

unread,
Jun 6, 2011, 4:55:28 AM6/6/11
to pywe...@googlegroups.com
When I say I have defined a static directory I of course meant STATIC_ROOT - I wrote this without the code in front of me.

Alex Arshavski

unread,
Jun 6, 2011, 5:02:27 AM6/6/11
to pywe...@googlegroups.com
Go to your views file and add:

from django.template import RequestContext

and in your view add:

return render_to_response("<your html>", {<your data>}, context_instance=RequestContext(request))

That should bring the variables from settings.py to your template.
Hope it helps.

Regards, Alex A.

On Mon, Jun 6, 2011 at 11:55 AM, towerofbabel <pauly...@gmail.com> wrote:
When I say I have defined a static directory I of course meant STATIC_ROOT - I wrote this without the code in front of me.

--
You received this message because you are subscribed to the Google Groups "PyWeb-IL" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pyweb-il/-/LXA3M3ZmbVE4SjRK.

To post to this group, send email to pywe...@googlegroups.com.
To unsubscribe from this group, send email to pyweb-il+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pyweb-il?hl=en.

Udi Oron

unread,
Jun 6, 2011, 6:08:59 AM6/6/11
to pywe...@googlegroups.com
Hi!

1.
Is django.contrib.staticfiles included in your INSTALLED_APPS ?

2.
An easier and more readable way to render template with the request context is using django-annoying's render_to decorator:

Udi

Meir Kriheli

unread,
Jun 6, 2011, 6:18:34 AM6/6/11
to pywe...@googlegroups.com
Hi

On Mon, Jun 6, 2011 at 1:08 PM, Udi Oron <udi...@gmail.com> wrote:
Hi!

1.
Is django.contrib.staticfiles included in your INSTALLED_APPS ?

2.
An easier and more readable way to render template with the request context is using django-annoying's render_to decorator:

Udi


There's no need for that. Django 1.3 has a render shortcut (plus I don't like decorators for this type of operation):

https://docs.djangoproject.com/en/1.3/topics/http/shortcuts/#render

And in Django < 1.3 the direct_to_template generic view could be used for the same thing.
 

Cheers
--
Meir

towerofbabel

unread,
Jun 6, 2011, 8:31:47 AM6/6/11
to pywe...@googlegroups.com
Thanks, it was that simple.

Alex Arshavski

unread,
Jun 6, 2011, 8:38:19 AM6/6/11
to pywe...@googlegroups.com
You're welcome :)

On Mon, Jun 6, 2011 at 3:31 PM, towerofbabel <pauly...@gmail.com> wrote:
Thanks, it was that simple.

--
You received this message because you are subscribed to the Google Groups "PyWeb-IL" group.
To view this discussion on the web visit https://groups.google.com/d/msg/pyweb-il/-/OTMwVnZ0V0hwdWtK.

towerofbabel

unread,
Jun 7, 2011, 4:17:01 AM6/7/11
to pywe...@googlegroups.com
Ah nice, that is even better!

Thanks.
Reply all
Reply to author
Forward
0 new messages