python-3.xdjangodjango-3.1

TemplateSyntaxError: Variable 'user.profile.photo' is an invalid source


I have problem with accessing by the link. I am using easy-thumbnail framework, and I created simple view for user list to list all existing users. Error message:

django.template.exceptions.TemplateSyntaxError: Variable 'user.profile.photo' is an invalid source.

Django Traceback throws me to this view.

views.py file:

@login_required
def user_list(request):
    users = User.objects.filter(is_active=True)
    return render(request,
                  'account/user/list.html',
                  {'section': 'people',
                   'users': users})

urls.py file:

path('users/', views.user_list, name='user_list'),

template list.html:

{% extends "base.html" %}
{% load thumbnail %}

{% block title %}People{% endblock %}

{% block content %}
  <h1>People</h1>
  <div id="people-list">
    {% for user in users %}
      <div class="user">
        <a href="{{ user.get_absolute_url }}">
          <img src="{% thumbnail user.profile.photo 180x180 %}">
        </a>
        <div class="info">
          <a href="{{ user.get_absolute_url }}" class="title">
            {{ user.get_full_name }}
          </a>
        </div>
      </div>
    {% endfor %}
  </div>
{% endblock %}

sample code from base.html:

 <li {% if section == "people" %}class="selected"{% endif %}>
 <a href="{% url "user_list" %}">People</a>
</li>

models.py:

class Profile(models.Model):
    user = models.OneToOneField(settings.AUTH_USER_MODEL,
                                on_delete=models.CASCADE)
    date_of_birth = models.DateField(blank=True, null=True)
    photo = models.ImageField(upload_to='users/%Y/%m/%d/',
                              blank=True)

    def __str__(self):
        return f'Profile for user {self.user.username}'

Thank you for the helping in advance.


Solution

  • Reviewed the documentation https://easy-thumbnails.readthedocs.io/en/latest/usage/#templates. Creates a thumbnail from an object (usually a file field). I tried it and it helped solve my problem:

     <img src="{{ user.profile.photo.url }}">