James L.
James L.

Reputation: 1153

Django 1.6 Profile pic not showing

I'm trying to display a picture from one of the models in a template, but I run into the following error. It seems like there is an error in the views but I'm not sure what to change. I feel like I'm passing the right parameters but it's not working.

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: []

Here is the template where I'm trying to load the picture

{% include "meddy1/header.html" %}
{% load staticfiles %}

<br>
<br>

<h1>{{doctor.name}}'s Profile</h1>
<img src="{% url 'getDocProfilePicture' doctor.id %}">

{% include "meddy1/footer.html" %}

Here is views.py

def getDocProfilePicture(request, id):
    d = Doctor.objects.get(id=doctor_id)
    return HttpResponse(d.profile_pic.read())

Here is 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'),

Upvotes: 0

Views: 161

Answers (1)

yedpodtrzitko
yedpodtrzitko

Reputation: 9359

you don't need View for this, just call the attribute with image:

<img src="{{ MEDIA_URL}}{{ doctor.profile_pic.url }}">

Upvotes: 1

Related Questions