Warm tip: This article is reproduced from serverfault.com, please click

django redirect url containing id

发布于 2010-06-03 11:07:24

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

Questioner
dana
Viewed
11
KillianDS 2010-06-03 19:17:00

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