Django and static files URLs

3 views
Skip to first unread message

dr.v...@laposte.net

unread,
May 17, 2006, 12:10:21 PM5/17/06
to Django User Group
Hi,

My templates refer to CSS and images with absolute URL. I
cannot serve them with relative URLs, like I used to do in PHP
with:
<img src="images/stuff.png">
(or can I? If so please let me know).

This is fine until the moment I put my work on production
server, where I need to update all static URLs (from
http://localhost:8080/media/... to
http://www.mystuff.com/media/...).

I came across a few posts dealing about static files and URLs,
usually ending up with some kind of request for a 'website
root URL' tag in templates.

Does such a thing exist in Django (without creating custom tag)?
Are there any works in progress?
More generally, what are the best practices for not having to
substitute all media URLs in my templates (without serving the
same files for dev and prod) ?

Thanks.

Accédez au courrier électronique de La Poste : www.laposte.net ;
3615 LAPOSTENET (0,34 €/mn) ; tél : 08 92 68 13 50 (0,34€/mn)

Phil Powell

unread,
May 17, 2006, 12:22:25 PM5/17/06
to django...@googlegroups.com
Surely you don't need to have the entire absolute URL - an sbsolute
URL can be defined by just ensuring a preceeding forward slash appears
in your paths - for example:

<img src="images/stuff.png">

should change to:

<img src="/images/stuff.png">

This will cause the resource to be referenced from the root of your domain.

Alternatively, you can use a BASE tag in the head of your HTML to set
a a full or partial base path to preceed all relative resources.

Hope that helps.

-Phil

Malcolm Tredinnick

unread,
May 17, 2006, 8:26:42 PM5/17/06
to django...@googlegroups.com
Hi,


On Wed, 2006-05-17 at 18:10 +0200, dr.v...@laposte.net wrote:
> Hi,
>
> My templates refer to CSS and images with absolute URL. I
> cannot serve them with relative URLs, like I used to do in PHP
> with:
> <img src="images/stuff.png">
> (or can I? If so please let me know).

Sure you can; creating the ultimate URL that gets referenced is handled
by your web browser, nothing at all to do with Django or PHP. Be aware
relative URLs are a little fiddly if you have a descriptive URL scheme:
if you have one URL that looks like /blog/latest/ and another that looks
like /blog/2006/05/, then your relative URL for the above image will try
to request /blog/latest/images/stuff.png in the first case
and /blog/2006/05/latest/images/stuff.png in the latter (or maybe I've
put in one too many path components in each case there... can't remember
exactly now). Obviously, you are not going to have an images/ directory
everywhere, since some of these things won't even correspond to paths on
your filesystem easily.

>
> This is fine until the moment I put my work on production
> server, where I need to update all static URLs (from
> http://localhost:8080/media/... to
> http://www.mystuff.com/media/...).

As mentioned in another post, just start your above path with a slash.
You don't need the hostname. So <img src="/images/stuff.png"> will end
up always being served from http://server.name.here/images/stuff.png.


> I came across a few posts dealing about static files and URLs,
> usually ending up with some kind of request for a 'website
> root URL' tag in templates.
>
> Does such a thing exist in Django (without creating custom tag)?
> Are there any works in progress?

A tag like this is not universally appropriate for Django, because one
Django installation does not always correspond to just one website (this
was something it took me a while to get my brain around in the early
days). So a single root URL does not always make sense. Maybe one of the
people you found earlier saying they needed this tag ended up creating
some code, I don't know.

> More generally, what are the best practices for not having to
> substitute all media URLs in my templates (without serving the
> same files for dev and prod) ?

Phil Powell mentioned one very common approach for using in templates,
that is independent of the server name (and portable). For generated
links that really need a hostname (such as pointing to another system),
it is not forbidden to add your own variables to settings.py and then
use that to generate the full URL (such as happens with MEDIA_URL. For
example, if all employee records were on one particular server, you
could have

EMPLOYEE_SERVER = 'http://some.internal.server/'

in settings.py and generate links in your view functions with

from django.conf import settings

...

def my_view(...):
...
target_url = settings.EMPLOYEE_SERVER +
object.get_absolute_url()

(or even retrieve settings.EMPLOYEE_SERVER inside the get_aboslute_url()
call if those objects will always be on that machine).

Hope this gives you some ideas. There is no single best way. Different
approaches work in different situations.

Regards,
Malcolm

dr.v...@laposte.net

unread,
May 18, 2006, 3:34:35 AM5/18/06
to django-users
It looks very clear now!

Driven by the simplicity of the tutorial, and by the buzz
around how time-saving web frameworks are (in general, Django
in particular), it took me a while to figure out that a bit of
customization was sometimes necessary.

Thanks Phil and Malcolm.

Reply all
Reply to author
Forward
0 new messages