我正在尝试在模板中显示其中一个模型的图片,但是我遇到了以下错误。似乎视图中有错误,但我不确定要改变什么。我觉得我传递了正确的参数,但它没有用。
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "views.py" in showDocProfile
51. return render(request,'meddy1/docprofile.html',{'doctor': profile})
File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py" in render
53. return HttpResponse(loader.render_to_string(*args, **kwargs),
File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string
169. return t.render(context_instance)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
140. return self._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
134. return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
840. bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
78. return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
447. six.reraise(*exc_info)
File "/Library/Python/2.7/site-packages/django/template/defaulttags.py" in render
433. url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse
532. return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
452. (lookup_view_s, args, kwargs, len(patterns), patterns))
Exception Type: NoReverseMatch at /docprofile/1/
Exception Value: Reverse for 'getDocProfilePicture' with arguments '(1,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
这是我正在尝试加载图片的模板
{% include "meddy1/header.html" %}
{% load staticfiles %}
<br>
<br>
<h1>{{doctor.name}}'s Profile</h1>
<img src="{% url 'getDocProfilePicture' doctor.id %}">
{% include "meddy1/footer.html" %}
这是views.py
def getDocProfilePicture(request, id):
d = Doctor.objects.get(id=doctor_id)
return HttpResponse(d.profile_pic.read())
这是models.py
class Doctor(models.Model):
name = models.CharField(max_length=30)
specialization = models.ForeignKey(Specialization)
clinic = models.ForeignKey(Clinic)
seekers = models.ManyToManyField(DoctorSeeker, through='Review')
language = models.ManyToManyField(Language)
education1 = models.CharField(max_length=100)
education2 = models.CharField(max_length=100, null = True)
gender_choices = ( ('M', 'Male'), ('F','Female'),)
gender = models.CharField(max_length=5, choices = gender_choices, null=True)
profile_pic = models.ImageField(upload_to='uploads/', null=True)
statement = models.TextField(null=True)
affiliation = models.CharField(max_length=100, null = True)
urls.py
url(r'^docprofile/(?P<id>\d+)/$', views.showDocProfile, name='showDocProfile'),
url(r'^getDocProfileicture/ (?P<doctor_id>\d+)/$', views.getDocProfilePicture, name='getDocProfilePicture'),
答案 0 :(得分:1)
您不需要View for this,只需使用image调用该属性:
<img src="{{ MEDIA_URL}}{{ doctor.profile_pic.url }}">