Display image from model to template Django -
the image can upload file "upload" located in mysite file, here code setting.py
media_root = 'c:/python26/mysite/upload/media/' media_url = '/media/' in url.py
urlpatterns = patterns('', url(r'^media/(?p<path>.*)$', 'django.views.static.serve', {'document_root': settings.media_root,}), ) in template
<img src="{{media_url}}/{{pic}}" alt="profile picture" width="200"/> however, image still not show in website, indeed stored in upload file. me that? thanks
given:
media_url = '/media/' and following template code:
<img src="{{media_url}}/{{pic}}" alt="profile picture" width="200"/> the image src couldn't possibly /upload/desert.jpg. path should starting /media/. more not media_url undefined , value of pic upload/desert.jpg. if that's case, you're missing media template context processor. change template_context_processors to:
template_context_processors = ( ... 'django.core.context_processors.media', ) that make media_url available in template context. then, you'll need remove slash after it, or you'll end 2 (media_url ends slash), i.e.:
instead of:
{{ media_url }}/{{ pic }} use:
{{ media_url }}{{ pic }} update
if you're still not getting value media_url, you're not using requestcontext. have wrap view's context in requestcontext in order template context processors thing.
if you're using render_to_response, then:
return render_to_response('template.html', { ... context here ... }, context_instance=requestcontext(request)) if you're using django 1.3+, can use render method, automatically:
return render('template.html', { ... context here ... })
Comments
Post a Comment