问题
I've been going around in circles for hours with this.
I have a UserProfile model with profile_pic ImageField. I saved the user photo and now I want to display it, but nothing works. I went through docs, SO, google, with no result.
I uploaded a photo, and I can see that the url was saved in the database.
This is what I have:
# models.py
class UserProfile(models.Model):
...
title = models.CharField()
about_me = models.CharField()
profile_pic = models.ImageField(upload_to=content_file_name, blank=True)
# views.py
def user_details(request):
if request.method == 'POST':
form = UserProfileForm(request.POST, request.FILES, instance=request.user.get_profile())
if form.is_valid():
form.save()
return HttpResponseRedirect('/')
else:
form = UserProfileForm(instance=request.user.get_profile())
return render_to_response('user_details.html',
{'user': request.user, 'form': form},
context_instance=RequestContext(request))
# user_details.html
{% for field in form %}
{% if field.name != "profile_pic" %}
<!-- display regular fields -->
{{ field.label_tag }}{{ field }}
{% else %}
<!-- display the user photo -->
<img src="{{ MEDIA_URL }}{{ field.url }}" />
{% endif %}
{% endfor %}
When I view the source of the rendered HTML, all I see is the MEDIA_URL (ie 'media/') but not the photo url. If I replace {{ field.url }} with {{ field }} I get the url with the label "Currently", a "Clear" checkbox and an option to change image. But I cannot see the image itself.
Can anyone suggest what am I doing wrong?
回答1:
Sorry, I didn't pay close enough attention to the context of field
. The "field" you get from the for loop is a specialized wrapper that includes other data about the field for the purposes of constructing the form. It's not the field on the model itself, just a representation of it for the purposes of the form.
To get the current value of profile_pic
, you need to use the form's instance:
<img src="{{ form.instance.profile_pic.url }}" />
{{ MEDIA_URL }}
is unnecessary when using the url
attribute, since it automatically appends MEDIA_URL
.
回答2:
This works for me:
{% for field in form %}
<p>
{{ field.errors }}
{{ field.label_tag }}
{% if field.field|get_type == "django.forms.fields.ImageField" and field.value %}
<br/><img src="{{ field.value.url }}" width="240"></img></br>
{% endif %}
{{ field }}
</p>
{% endfor %}
get_type is a templatetag defined as:
from django import template
register = template.Library()
@register.filter
def get_type(value):
t = type(value)
return t.__module__ + "." + t.__name__
来源:https://stackoverflow.com/questions/9830183/django-how-can-i-display-a-photo-saved-in-imagefield