Add id to url after login user

70 views
Skip to first unread message

Dariusz Mysior

unread,
Apr 29, 2016, 3:36:30 AM4/29/16
to Django users
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/'   

Michal Petrucha

unread,
Apr 29, 2016, 4:03:50 AM4/29/16
to django...@googlegroups.com
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
signature.asc

Dariusz Mysior

unread,
Apr 29, 2016, 4:12:23 AM4/29/16
to Django users
Hi I try learn Django, and I want do authenticated system in CBV. My nxt try is that I wright it like below but in url adrees instead of id number I have "None" :/
 success_url = '/users/profile/'+ str(MysiteUser.pk)

Michal Petrucha

unread,
Apr 29, 2016, 4:44:28 AM4/29/16
to django...@googlegroups.com
On Fri, Apr 29, 2016 at 01:12:23AM -0700, Dariusz Mysior wrote:
> Hi I try learn Django, and I want do authenticated system in CBV. My nxt
> try is that I wright it like below but in url adrees instead of id number I
> have "None" :/
>
> success_url = '/users/profile/'+ str(MysiteUser.pk)

That won't work – the class definition of LoginView is evaluated
during import, not when processing a request, which means you cannot
use the “current user” object in the definition of a class attribute.

Is there any reason why you cannot use one of the two solutions I
described in my previous email? (Quoted below.)
signature.asc

Dariusz Mysior

unread,
Apr 29, 2016, 10:18:28 AM4/29/16
to Django users
Hej ja po angielsku słabo piszę, chciałem się nauczyć Class Based View i na tym zrobić system rejestracji, masz pomysł jak to dalej zrobić? Chciałbym wyciagnąć pk i dodać do adresu succes_url...


W dniu piątek, 29 kwietnia 2016 09:36:30 UTC+2 użytkownik Dariusz Mysior napisał:

Michal Petrucha

unread,
Apr 29, 2016, 10:38:48 AM4/29/16
to django...@googlegroups.com
On Fri, Apr 29, 2016 at 07:18:28AM -0700, Dariusz Mysior wrote:
> Hej ja po angielsku słabo piszę, chciałem się nauczyć Class Based View i na
> tym zrobić system rejestracji, masz pomysł jak to dalej zrobić? Chciałbym
> wyciagnąć pk i dodać do adresu succes_url...

Heh, Michal, nie Michał. (-: O krajinu vedľa (Slovensko).

Anyway, while I can read Polish more or less OK, I'm clueless at
writing, so I'll try this in English again.

What I suggest is that you leave your view as it was, and create
another one just to redirect the user::

class LoginView(FormView):
template_name = 'login_form.html'
model = MysiteUser
form_class = AuthenticationForm
success_url = '/users/profile/'


class CurrentUserProfileView(RedirectView):
def get_redirect_url(self):
return reverse('profile-view', pk=self.request.user.pk)

And, of course, then you have to add it to urls.py::

from users.views import RegisterView, LoginView, ProfileView, CurrentUserProfileView

urlpatterns = [
url(r'^register/$', RegisterView.as_view(), name='register-view'),
url(r'^login/$', LoginView.as_view(), name='login-view'),
url(r'^profile/$', CurrentUserProfileView.as_view(), name='current-profile-view'),
url(r'^profile/(?P<pk>\d+)/$', ProfileView.as_view(), name='profile-view'),
]

Michal
signature.asc

Dariusz Mysior

unread,
Apr 29, 2016, 3:06:02 PM4/29/16
to Django users
Sory I thought that You are from Poland like I :)

Hmm I try Your code but there is comment
name 'request' is not defined

W dniu piątek, 29 kwietnia 2016 09:36:30 UTC+2 użytkownik Dariusz Mysior napisał:

Michal Petrucha

unread,
Apr 30, 2016, 5:56:21 AM4/30/16
to django...@googlegroups.com
On Fri, Apr 29, 2016 at 12:06:02PM -0700, Dariusz Mysior wrote:
> Sory I thought that You are from Poland like I :)
>
> Hmm I try Your code but there is comment
>
> name 'request' is not defined

It's hard to tell what's wrong without seeing the full traceback that
you get with that error; could you paste it here?

Cheers,

Michal
signature.asc

Dariusz Mysior

unread,
Apr 30, 2016, 10:13:14 AM4/30/16
to Django users
I have now code like below and I have message. I try split success_url with id number and compare it with urls.py


AttributeError at /users/login/

'int' object has no attribute 'get'


views.py
class LoginView(FormView):
    template_name = 'login_form.html'
    model = MysiteUser
    form_class = AuthenticationForm

def form_valid(self, form):
x = form.get_user_id()
return x
def get_success_url(self):
x = self.x
success_url = '/users/profile/{}'.format(x)
return success_url



W dniu piątek, 29 kwietnia 2016 09:36:30 UTC+2 użytkownik Dariusz Mysior napisał:

urls.py

from django.conf.urls import   url

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<id>[0-9]+)/$', ProfileView.as_view(), name='profile-view'),

]

 
Reply all
Reply to author
Forward
0 new messages