1

I am trying to get django to show pictures. Ive installed PIL and it seams to work. i am running py 2.7, win 7, the developement server and sqlite3

my static root:

STATIC_ROOT = r'H:/netz2/skateproject/static/'

ive got a background picture in this folder and can acces it via

http://127.0.0.1:8000/static/images/lomax.jpg

now im trying to upload a pic via an image field & the admin. it works fine, the picture lands in the right folder (ive changed the folders a million times by now to see if that could be the problem) but the picture can not be displayed.

part of my model:

class Sk8(models.Model):
    name        = models.CharField(max_length=50, default='name of the model')
    bild1       = models.ImageField(upload_to="uploads/")

my media root:

MEDIA_ROOT = r'H:/netz2/skateproject/media/'

the picture lands in the right folder. netz/skateproject/media/pic.jpg

but it seams like localhost can not acces this folder. when i try - i get an 404 and django tells me that the url does not match anything i defined in my urls. which is true - but neither did I define a url for the background image in the static folder - and i can access this one just fine.

ive also tried to acces the file from my html template, like this:

{% extends "grundgerüst.html" %}
{% block inhalt %}
    <div id='skatelist'>
        {% for sk8 in skates %}
        <p><a href="/skates/{{sk8.slug}}/">{{sk8}}</a></p>
        <p><img src="{{sk8.bild.url}}"/></p>
        {% endfor %}
    </div>
{% endblock %}

obviously the picture does not show up.

I dont know what is wrong here. i guess i shoudl be able to see the pic on localhost? heres a pic of the error chrome gives me

enter image description here

if i.e. in the picture the folders are different than the ones in the code example its because i changed them testing different settings.

I really hope somebody can help me to figure out image_field, cause uploading pictures if i cant display them does not make much sense :)

thanks in advance!!! danielll

ouh and - i already tried google and django reference etc. i honestly could not figure it out that way. every help appreciated!

Mathew Thompson
  • 55,877
  • 15
  • 127
  • 148
Daniel Prell
  • 273
  • 1
  • 2
  • 8

1 Answers1

1

Django's contrib.staticfiles app will monkeypatch your urls to serve static files when in DEBUG mode [ref]. Unfortunately the same cannot be said of media files.

Add the following snippet to your project urls.py to display media

django.conf import settings

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

Docs reference

Thomas
  • 11,757
  • 4
  • 41
  • 57
  • Awesome! that was it! perfect answer, thank you! if somebody else has this problem dont forget to include: from django.conf import settings in your urls or youll receive an error first. if you need better understanding of it check the link to the django documentation Thomas posted. thanks thomas - saved my day! – Daniel Prell Jan 07 '13 at 15:22