在我正在构建的Django应用程序中,我希望用户创建过程如下:当用户注册时,如果有效,则重定向以创建LIST对象,如果有效,则重定向到将是刚刚创建的LIST对象的仪表板。我的views.py如下:
def user_signup(request):
if request.method == 'POST':
form = forms.UserSignupForm(data=request.POST)
if form.is_valid():
user = form.save()
g = Group.objects.get(name='test_group')
g.user_set.add(user)
# log user in
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
user = authenticate(username=username, password=password)
login(request, user)
messages.success(request, u'Welcome to Social FollowUp')
return redirect('user_create')
else:
form = forms.UserSignupForm()
return TemplateResponse(request, 'user_signup.html', {
'form': form,
})
@login_required
@permission_required('')
def user_create(request):
if request.method == 'POST':
list_form = forms.ListForm(request.POST)
if list_form.is_valid():
list_create = list_form.save()
messages.success(request, 'List {0} created'.format(list_create.list_id))
return redirect('user_dashboard')
else:
list_form = forms.ListForm()
return TemplateResponse(request, 'dashboard/create.html', {'list_form': list_form, })
def user_dashboard(request, list_id):
try:
list_id = models.List.objects.get(pk=list_id)
except models.List.DoesNotExist:
raise Http404
return TemplateResponse(request, 'dashboard/view.html', {'list_id': list_id})
我对这些观点的urls.py如下:
url(r'user/signup/$', views.user_signup, name='user_signup'),
url(r'u/dashboard/(?P<list_id>\d+)/$', views.user_dashboard, name='user_dashboard'),
url(r'u/list/create/$', views.user_create, name='user_create'),
当我尝试运行该过程时,前两个视图正常工作。但是,当我重定向到user_dashboard时,我收到以下错误:
Reverse for 'user_dashboard' with arguments '' and keyword arguments '{}' not found.
哪个网站:
return redirect('user_dashboard')
我假设这与我没有传入list_id有关,但是,即使我试图传入一个硬编码的值它也不起作用(像这样):
return redirect('user_dashboard', {'list_id': 2})
我在这里做错了什么?
答案 0 :(得分:3)
尝试:
return redirect(reverse('user_dashboard', args=(2,)))
您的代码
return redirect('user_dashboard')
不起作用,因为在你的网址模式中,你有
url(r'u/dashboard/(?P<list_id>\d+)/$', views.user_dashboard, name='user_dashboard'),
需要 list_id
作为参数。