Friday, April 29, 2016

Re: Add id to url after login user

On Fri, Apr 29, 2016 at 12:36:30AM -0700, Dariusz Mysior wrote:
> I use FormView do login user, but I don't know how I should add his ID
> number to success url that when he will log in adres will be
> users/profile/id
>
> urls.py
>
> from users.views import RegisterView, LoginView, ProfileView
>
> urlpatterns = [
>
> url(r'^register/$', RegisterView.as_view(), name='register-view'),
> url(r'^login/$', LoginView.as_view(), name='login-view'),
> url(r'^profile/(?P<pk>\d+)/$', ProfileView.as_view(), name='profile-view'),
>
>
>
> views.py
>
> class LoginView(FormView):
> template_name = 'login_form.html'
> model = MysiteUser
> form_class = AuthenticationForm
> success_url = '/users/profile/'

Have you considered using the built-in ``login`` view? [1] One of its
advantages is that it has built-in support for the ``next`` URL
argument, which makes it possible to redirect the user back to the
page they were trying to access before they were asked to log in,
instead of always redirecting them to the same fixed page after login.

Regarding the meat of your question, there are several options, but
personally, I'd set up an additional view with a URL pattern of
'^profile/$' that would just get the current logged-in user, and
return a redirect to the user's profile page.

Or even better, if you do not need users to be able to view other
users' profiles, you can just remove the user ID from your profile URL
pattern, and simply always display the current user's profile there.

Cheers,

Michal


[1] https://docs.djangoproject.com/en/1.9/topics/auth/default/#django.contrib.auth.views.login

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20160429080314.GE435%40koniiiik.org.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment