[Answered ]-Why my code for displaying user profile in Django isn't working?

1👍

In your profile_list.html you have to include the app name since you defined it in your urls.py, app_name = 'app':
profiles_list.html

<a href="{% url 'app:user_profile' profile.user.id %}">

Next, in your user_profile.html it should be

{% extends 'base.html' %}

{% block body %}
  <div class="profile">
    <img src="{{ profile.profile_pic.url }}" alt="Zdjęcie profilowe użytkownika {{ profile.user.username }}">
    <h2>{{ profile.user.username }}</h2>
    <p>{{ profile.bio }}</p>
  </div>
{% endblock %}

Why? Because in your user_profile view you send profile in your context, return render(request, 'user_profile.html', {'profile': profile}). Now, profile is a UserProfile object you defined in your models.py with fields of bio and profile_pic, and to access the user name you do profile.user.username.

Leave a comment