简体   繁体   中英

Django redirect() with additional parameters

Why is it that when I try to pass additional parameters, I get the error:

The below works fine! (Without parameters)

def first(request):
    return redirect('confirm')

def confirm(request):
    return render(request, 'template.html')

However this is what I need, which doesn't work as expected, and I get the error:

Reverse for 'confirm' with keyword arguments '{'email': 'email'}' not found. 1 pattern(s) tried: ['book/booking_confirmation/$']

def first(request):
    return redirect('confirm', email='some_email')

def confirm(request, email):
    return render(request, 'template.html', { 'email': email} )

urls.py

urlpatterns += [
    url(r'^booking_confirmation/$', views.confirm, name='confirm'),
]

If you don't want to pass the email via the URL in the redirect, it may be easiest to store it in the session.

def first(request):
    request.session['email'] = 'some_email'
    return redirect('confirm')

def confirm(request):
    return render(request, 'template.html', {'email': request.session['email']})

Configure the url pattern to capture the email. eg

urlpatterns += [
    url(r'^booking_confirmation/(?P<email>\w+)$', views.confirm, name='confirm'),
]

You can use more robust pattern-match for the captured email instead of capturing only word characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM