Вопрос

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!

Это было полезно?

Решение

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top