Answer the question
In order to leave comments, you need to log in
Viewing a Django User Profile?
Good day! I can not figure out how to implement viewing someone else's profile. There is a slightly modified user model, login form:
class LoginFormView(FormView):
form_class = AuthenticationForm
template_name = "login.html"
success_url = "/"
def form_valid(self, form):
self.user = form.get_user()
login(self.request, self.user)
return super(LoginFormView, self).form_valid(form)
def form_invalid(self, form):
return super(LoginFormView, self).form_invalid(form)
class ProfileView(LoginRequiredMixin, DetailView):
model = User
template_name = 'profile.html'
pk_url_kwarg = 'user_pk'
class UpdateProfileView(LoginRequiredMixin, UpdateView):
form_class = UpdateForm
model = User
template_name = 'profile_update.html'
pk_url_kwarg = 'user_pk'
def get(self, request, *args, **kwargs):
user = User.objects.get(pk=self.kwargs['user_pk'])
if user != request.user:
return HttpResponseForbidden()
else:
return super(UpdateProfileView, self).get(request, *args, **kwargs)
def get_success_url(self):
return reverse('profile_update', kwargs={'user_pk': self.kwargs['user_pk']})
url(r'^profile/(?P<user_pk>\d+)/$', ProfileView.as_view(), name='profile')
<li><a href="{% url 'profile' user_pk=user.pk %}">профиль</a></li>
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