How can I redirect to a view with a parameter?
I'm trying to follow the instructinons for redirect in Django. I want to redirect to a page and then redirect back again. Here's my code开发者_运维百科:
def home(request):
current_user = None
if 'user_id' in request.session.keys():
current_user = request.session['user_id']
else:
return redirect('update_session_from_ldap', next='home')
def update_session_from_ldap(request,next):
remote_user = request.META.get('REMOTE_USER', None)
request.session['user_id'] = remote_user
request.session.set_expiry(0) # Expire with browser session.
return redirect('home')
When I go to the homepage, I get:
NoReverseMatch at / Reverse for 'update_session_from_ldap' with arguments '()' and keyword arguments '{'next': 'home'}' not found.
What am I doing wrong?
what you're doing in the view is correct, it's just that the parameter next
is missing from the url:
url(r'^/ldap/(?P<next>\w+)/$',
'library.views.update_session_from_ldap',
name="ldap")
But I see that you're not using the next
parameter in update_session_from_ldap
. If you want to always redirect to 'home' and ignore next
, remove it from the view and keep your original url
.
In addition to what manji says, you need to either use the name of the URL, or the full function reference. You can't just use the last part of the function reference.
So, either:
return redirect('library.views.update_session_from_ldap', next='home')
or
return redirect('ldap', next='home')
精彩评论