'Display a different user's information in a profile (Not the logged in user's)
I'm working on a project for school, it's sort of mini-stackoverflow. So, I have a section where users are able to click on the creator of a post, and it'll take then to a page where they can see said user's information and all the posts they have created. So, so far the code for the user posts list is perfect but I can't get the profile information to be displayed correctly.
To explain the problem in a more simple way, it's basically that, if I click on winston1420's profile instead shows me a different profile (of the logged in user)
My profile on HTML template
<section class="py-5">
<div class="container my-5">
<div class="row justify-content">
<div class="col-lg-6">
<div class="content-section">
<div class="media">
<img class="rounded-circle profile-img" src="{{ user.profile.image.url }}"/>
<div class="media-body">
<h2 class="account-heading">{{ view.kwargs.username }}</h2>
<p class="text-secondary">{{ user.first_name }} {{ user.last_name }}</p>
<p class="text-secondary">{{ user.email }}</p>
<div class="container">
<p class="lead"><Strong>Sobre mi:</strong></p>
<p class="lead">{{ user.description }}</p>
</div>
<br>
<p class="text-secondary">Se unió el {{ user.date_joined }}</p>
<p class="text-secondary">Última vez visto: {{ user.last_login }}</p>
<p class="mb-0">{{ user.profile.about }}</p>
</div>
</div>
</div>
</div>
</div>
My user's post list
<div class="container my-5">
<div class="row justify-content">
<div class="col-lg-6">
<h2>Publicaciones de <strong><a href="">{{ view.kwargs.username }}</a></strong> ({{ page_obj.paginator.count }}) </h2>
<br>
</div>
</div>
{% for post in posts%}
<article class="media post-content-section">
<img class="rounded-circle article-img" src="{{ post.author.profile.image.url }}">
<div class="media-body">
<div class="article-metadata">
<a class="mr-2" href="{% url 'user-posts' post.author.username %}">{{ post.author }}</a>
<small class="text-muted">{{ post.date_posted|date:"j / m / Y" }}</small>
</div>
<h2><a class="article-title" href="{% url 'post-detail' post.id %}">{{ post.titulo }}</a></h2>
<!-- <p class="article-content">{{ post.contenido }}</p> -->
</div>
</article>
{% endfor %}
<br>
{% if is_paginated %}
<Center>{% if page_obj.has_previous %}
<a class="btn btn-outlife-info mb-4" href="?page=1">Primera Página</a>
<a class="btn btn-outlife-info mb-4" href="?page={{ page_obj.previous_page_number }}">Página Anterior</a>
{% endif %}
{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<a class="btn btn-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<a class="btn btn-outline-info mb-4" href="?page={{ num }}">{{ num }}</a>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<a class="btn btn-outlife-info mb-4" href="?page={{ page_obj.next_page_number }}">Siguiente Página</a>
<a class="btn btn-outlife-info mb-4" href="?page={{ page_obj.paginator.num_pages }}">Última Página</a>
{% endif %}</center>
{% endif %}
</div>
User Views
@login_required
def profile(request):
if request.method == 'POST':
u_form = UserUpdateForm(request.POST, instance=request.user)
p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile)
if u_form.is_valid() and p_form.is_valid():
u_form.save()
p_form.save()
messages.success(request, f'¡Tu perfil se ha actualizado correctamente!')
return redirect('profile')
else:
u_form = UserUpdateForm(instance=request.user)
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'u_form': u_form,
'p_form': p_form
}
return render(request, 'users/profile.html', context)
Post list Views
class PostListView(ListView):
model = Post
template_name = 'forum/forum_main.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 5
class UserPostListView(ListView):
model = Post
template_name = 'forum/user_posts.html'
context_object_name = 'posts'
paginate_by = 5
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return Post.objects.filter(author=user).order_by('-date_posted')
So, as you guys can see, for the post list I was using kwargs but I can't do the same for the profile, aside from the username which I already did. How can I do it?
Tips or links to documentation are appreciated as well.
Here is an image for reference:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
