1

I'm building a blog app with multi users and i'd like to have this ability that every logged user could click on every username (post author) and see details of this profile my current function gives me only details for current logged in user no mattew what user i click. i have similar CBV function working for post list view that shows me all the posts for each user and i've tried to do the same but it didn't worked.. Please help ;)

view

> @login_required def profile_detail(request): context = { 'profile': Profile.objects.all() } return render(request, 'users/user_detail.html', context) 

model

class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) image = models.ImageField(default='default.jpg', upload_to='profile_pics') def __str__(self): return f'{self.user.username} Profile' def save(self, *args,**kwargs): super().save(*args,**kwargs) img = Image.open(self.image.path) if img.height >300 or img.width > 300: output_size = (300, 300) img.thumbnail(output_size) img.save(self.image.path) 

url pattern

path('profile-detail/', user_views.profile_detail, name='user_detail'), 

link

<a class="mr-2" href="{% url 'user_detail' %}">{{ post.author }}</a> 
1
  • is post.author a Profile or a User? Commented Apr 26, 2020 at 19:33

3 Answers 3

1

Showing the profile of the logged in user

You obtain the Profile object for the logged in user with:

from django.shortcuts import get_object_or_404 @login_required def profile_detail(request): context = { 'profile': get_object_or_404(Profile, user=request.user) } return render(request, 'users/user_detail.html', context)

Showing the profile of the author

If you want to show the profile of the author, then you should pass it to the url. You thus can make a path that looks like:

urlpatterns = [ # …, path('profile/<int:pk>', views.profile_detail, name='user_detail') # …, ]

in your template, you can render the url with:

<a class="mr-2" href="{% url 'user_detail' pk=post.author.pk %}">{{ post.author }}</a>

and in the view, you can then use the primary key of that user:

from django.shortcuts import get_object_or_404 @login_required def profile_detail(request, pk): context = { 'profile': get_object_or_404(Profile, pk=pk) } return render(request, 'users/user_detail.html', context)
Sign up to request clarification or add additional context in comments.

7 Comments

it still gives me only current logged user profile even if i click on some other profile link
@gucisz: did you use the second solution? Note that you need to specify the {% url 'user_detail pk=post.author.pk %} for the links to these profiles.
@gucisz: what is the url you use to visit when looking at a profile?
i have only one link cuz i have a list of posts edited by for loop in this template
@gucisz: what I mean is, what link is generated to that profile. What URL do you visit when clicking the profile page of an author?
|
0

all of URL patterns

urlpatterns = [ path('admin/', admin.site.urls), path('register/', user_views.register, name='register'), path('profile/', user_views.profile, name='profile'), path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'), path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'), path('password-reset/', auth_views.PasswordResetView.as_view(template_name='users/password_reset.html'), name='password_reset'), path('password-reset/done/', auth_views.PasswordResetDoneView.as_view(template_name='users/password_reset_done.html'), name='password_reset_done'), path('password-reset-confirm/<uidb64>/<token>/', auth_views.PasswordResetConfirmView.as_view(template_name='users/password_reset_confirm.html'), name='password_reset_confirm'), path('password-reset-complete/', auth_views.PasswordResetCompleteView.as_view(template_name='users/password_reset_complete.html'), name='password_reset_complete'), path('profile-detail/<int:pk>/', user_views.profile_detail, name='user_detail'), path('', include('blog.urls')), 

]

1 Comment

@ Willem Van Onsem those are all of my url patterns
0

I've tryed and tryed and i've found solution i had to define this function like this now it works perfect functon

@login_required def profileDetail(request, pk): context = { 'user': get_object_or_404(User, pk=pk) } return render(request, 'users/user_detail.html', context) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.