A
A
Artur Kudashev2018-10-21 18:15:29
Django
Artur Kudashev, 2018-10-21 18:15:29

How to fix problem getting user id in Django?

Django replaces the user id with the id of the logged in user.

path('profile/<pk>/', UserProfile.as_view(), name = 'profile_url'),

class UserProfile(View):

  def get(self, request, pk):
    user = User.objects.get(pk = pk)
    username = user.username
    name = user.first_name
    surname = user.last_name
    last_online = user.last_login
    categories = Category.objects.all()
    return render(request, 'profile.html', {'user': user, 
                        'username': username, 
                        'name': name,
                        'surname': surname, 
                        'last_online': last_online, 
                        'categories': categories})

<a class="nav-link" href="{% url 'profile_url' pk=user.id %}">Профиль</a>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-10-21
@archi_kud

The context processor django.contrib.auth.context_processors.authoverrides your context variable with userits own. Rename it to something else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question