Reputation: 801
Whenever i tried to view other people profile, it always return my own profile again, i don't really know what's wrong and i have tried calling request.user
but it seems not to work
views.py
def UserProfile(request, username):
user = get_object_or_404(User, username=username)
profile = Profile.objects.get(user=user)
url_name = resolve(request.path).url_name
context = {
'profile':profile,
'url_name':url_name,
}
return render(request, 'userauths/profile.html', context)
urls.py main project
from userauths.views import UserProfile
urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('userauths.urls')),
path('<username>/', UserProfile, name='profile'),
]
index.html
{% for creator in creators %}
<a href="{% url 'profile' creator.user %}"><img class="avatar-img rounded-circle" src="{{creator.user.profile.image.url}}" alt="creators image" ></a></div>
<h5 class="card-title"><a href="{% url 'profile' creator.user %}">
{{creator.user.profile.first_name}} {{creator.user.profile.last_name}} {% if creator.verified %} <i class="bi bi-patch-check-fill text-info smsall" ></i> {% else %}{% endif %} </a></h5>
<p class="mb-2">{{creator.user.profile.bio}}</p></div>
{% endfor %}
Upvotes: 1
Views: 1252
Reputation: 801
I just fixed it now, in my profile.html, i am using {{request.user.profile.first_name}}
to display the informations. I was supposed to use {{profile.first_name}}
etc, and that was it.
Upvotes: 1
Reputation: 3860
Instead of this:
<a href="{% url 'profile' creator.user %}">
you should try:
<a href="{% url 'profile' creator.user.username %}">
From the presumably working statement get_object_or_404(User, username=username)
I assume that username
is a field (hopefully, unique) of the User
. So you should pass it onto the URL, not the entire User
model.
Upvotes: 0