views:

2817

answers:

4

I'm new to Django and I'm trying to learn it through a simple project I'm developing called 'dubliners' and an app called 'book'. The directory structure is like this:

dubliners/book/  [includes models.py, views.py, etc.]
dubliners/templates/book/

I have a JPG file that needs to be displayed in the header of each Web page. Where should I store the file? Which path should I use for the tag to display it using a template? I've tried various locations and paths, but nothing is working so far.

...

Thanks for the answer posted below. However, I've tried both relative and absolute paths to the image, and I still get a broken image icon displayed in the Web page. For example, if I have an image in my home directory and use this tag in my template:

<img src="/home/tony/london.jpg" />

The image doesn't display. If I save the Web page as a static HTML file, however, the images display, so the path is correct. Maybe the default Web server that comes with Django will display images only if they're on a particular path?

+5  A: 

In production, you'll just have the HTML generated from your template pointing to wherever the host has media files stored. So your template will just have for example

<img src="../media/foo.png">

And then you'll just make sure that directory is there with the relevant file(s).

during development is a different issue. The django docs explain it succinctly and clearly enough that it's more effective to link there and type it up here, but basically you'll define a view for site media with a hardcoded path to location on disk.

Right here.

JosefAssad
Probably better to use <img src="{{MEDIA_URL}}foo.png/>rather than hard coding the location of your media.
Randle Taylor
I tried this ... see my comment above.
Tony
Ignore the previous, I understand now - I read the page on serving static files that you linked to.
Tony
+1  A: 

Your

<img src="/home/tony/london.jpg" />

will work for a HTML file read from disk, as it will assume the URL is file:///home/.... For a file served from a webserver though, the URL will become something like: http://www.yourdomain.com/home/tony/london.jpg, which can be an invalid URL and not what you really mean.

For about how to serve and where to place your static files, check out this document. Basicly, if you're using django's development server, you want to show him the place where your media files live, then make your urls.py serve those files (for example, by using some /static/ url prefix).

Will require you to put something like this in your urls.py:

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

In production environment you want to skip this and make your http server (apache, lighttpd, etc) serve static files.

kender
Yes, I see - thanks.
Tony
A: 

I have exactly the same question:

How do I serve images in templates?

I'm using :

< src="{{art.image.url}}" alt= "" width="200" height="100" >

which returns the correct url : /media/jessica100.jpg but I get a broken image link.

(it can see the url but not display the image)

It's driving me crazy, I've been searching two days for an answer and am going round in circles...

It server strings correctly like 'title' but not images.

I also have the same problem in the admin area, when an uploaded image is clicked on it gives a page error/ doesnt display image.

Any help greatly appreciated.

+4  A: 

Hi,

I found the solution.

settings.py

MEDIA_ROOT = '<your_path>/media'
MEDIA_URL = 'http://localhost:8000/media/'

urls.py

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

.html

<img src="{{ <your_object>.image.url }}" />

I hope, this helps you.

Thanx much for this! Finally the only answer that actually helped me!
Jacques Bosch