views:

63

answers:

4

hello, i want to add in my settings.py a declaration like:

LOGIN_REDIRECT_URL='^private_profile/(?P<id>\d+)/$'
#or 
LOGIN_REDIRECT_URL='/accounts/private_profile/id/'

so that when the user with the id 1, for example,is logging in, he will be redirected to

LOGIN_REDIRECT_URL='/accounts/private_profile/1/'

but both alternatives,

LOGIN_REDIRECT_URL='^private_profile/(?P<id>\d+)/$'
#or 
LOGIN_REDIRECT_URL='/accounts/private_profile/id/'

are wrong, because in my browser i don't see the current user id, where am i wrong? Thanks

+3  A: 

When a user is logged, the request passed as the first parameter of your view has a user field. So in your view, you will know which user is logged:

def userProfile(request):
    print request.user

In My projets, I never specify the username in the urls because the User is already known in your code.

dzen
yes, now i see you are right. i've just deleted the id parameter, and work perfectly. thanks!
dana
+1  A: 

You could set your LOGIN_REDIRECT_URL to a view that checks for the id of the currently logged-in user. This view can then redirect the user to the correct location.

Login -> View checks current user id -> redirects to the correct page using user id

adamnfish
+1  A: 

One trick to accomplish this would be defining a generic view for everybody and linking them to that, this view could then look like:

from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

@login_required
def after_login(request):
    return HttpResponseRedirect('/accounts/private_profile/%d/'%request.user.id)

But as said in my comment, this is only if you really want the user id in the url, which is most of the time not necessary at all. As you can see in my view, you can get the user id from the request context, provided django.contrib.auth.context_processors.auth is added to the TEMPLATE_CONTEXT_PROCESSORS setting in settings.py

KillianDS
A: 

I did it this way.

#========== Login View ==========#
def login(request):
   #==login stuff==#
   return HttpResponseRedirect('/accounts/private_profile/'+user.id+'/')

#========== PROJECT urls.py ==========#
urlpatterns = patterns('',
   (r'^accounts/', include('my_project.accounts.urls')),
   (r'^login/', login),
)

#========== ACCOUNTS APP urls.py ==========#
urlpatterns = patterns('',
   (r'^private_profile/(?P<id>\d+)/', private_profile),
)
fuSi0N
thank you very much! It is maybe correct, but if you put the id in the url, your application is very vulnerable. if it is a public app, anybody can access any id, just by changing the url. Anyway, it is harder to make it secure. read the above answers. they helped me a lot.
dana
You can use decorators to check if user is_authenticated.I'm having the same problem:http://stackoverflow.com/questions/2958597/login-url-using-authentication-information-in-django
fuSi0N