Answer the question
In order to leave comments, you need to log in
How to create a profile page?
Hello, I want to do something similar to VK, I only did registration and login, the next step is to do it so that after logging in the user is redirected to his profile page. I decided to implement it through pk, but it gives an error:
Reverse for 'profile' with keyword arguments '{'pk': ''}' not found. 1 pattern(s) tried: ['profile\\/(?P[0-9]+)\\/$']
#views.py
def list_user(request):
list = Profile.objects.all()
return render(request, 'account/list.html', {'list': list})
def user_detail(request, pk):
profile = get_object_or_404(Profile, pk=pk)
return render(request, 'account/profile.html', {'profile': profile})
#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)
def __str__(self):
return self.user.username
#urls.py
path('list/', views.list_user, name='list'),
path('profile/<int:pk>/', views.user_detail, name='profile')
#list.html
{% for i in list %}
<a href="{% url 'profile' pk=profile.pk %}">{{ i.user.first_name }}</a><br><br>
{% endfor %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question