用户成功注册帐户(创建用户名和密码)后,网页会返回一个空白的注册表。
我想在注册成功后重定向到'landingpage'网址。
这是html表单:
<form method="post" action="{% url 'register' %}">
{% csrf_token %}
<table>{{ form.as_table }}</table>
<input type="submit" value="register" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
和urls.py:
from django.views.generic.edit import CreateView
from django.contrib.auth.forms import UserCreationForm
urlpatterns = [
url('^accounts/register/', CreateView.as_view(
template_name='registration/register.html',
form_class=UserCreationForm,
success_url='landingpage'
), name='register'),
url('^accounts/', include('django.contrib.auth.urls')),
url(r'^$', views.landingpage, name='landingpage'), ]
success_url ='landingpage'应该重定向到目标网址吗?
成功注册后,如何重定向到目标网址?
答案 0 :(得分:2)
您正在寻找reverse_lazy
功能,它包含在django.core.urlresolvers
内,必须使用from django.core.urlresolvers import reverse_lazy
代码应类似于:
from django.core.urlresolvers import reverse_lazy
# more code goes here
url('^accounts/register/', CreateView.as_view(
template_name='registration/register.html',
form_class=UserCreationForm,
success_url=reverse_lazy('landingpage') # note the usage of reverse_lazy here
), name='register'),
# also here
该功能将遍历您的网址,并在成功注册后进行正确的重定向。
请查看django文档以获取更多选项:http://www.beyondlogic.org/usbnutshell/usb5.shtml