Django-使用参数重定向视图 [英] Django -- Redirecting Views with Parameters

查看:62
本文介绍了Django-使用参数重定向视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在构建的Django应用程序中,我希望用户创建过程如下:当用户注册时,如果有效,则重定向到创建LIST对象,如果有效,则重定向到将是刚刚创建的LIST对象的仪表板.我的views.py如下:

In the Django app I am building I would like to have the user creation process go as follows: As user signs up, if valid is then redirected to create a LIST object, and if valid is then redirected to what will be a dashboard for the LIST object just created. My views.py are as follows:

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如下:

My urls.py for these views is as follows:

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时,出现以下错误:

When I try to run through the process, the first two views work correctly. However when I redirect to the user_dashboard I get the following error:

Reverse for 'user_dashboard' with arguments '' and keyword arguments '{}' not found.

该站点位于以下位置:

return redirect('user_dashboard')

我假设这与我没有传递list_id有关,但是,即使我尝试传递硬编码值也不起作用(像这样):

I'm assuming this has something to do with me not passing in a list_id, however, even when I tried to pass in a hardcoded value it did not work (like this):

return redirect('user_dashboard', {'list_id': 2}) 

我在这里做错了什么?

推荐答案

尝试:

return redirect(reverse('user_dashboard', args=(2,)))

您的代码

return redirect('user_dashboard')

无效,因为在您的url模式中,您拥有

would not work because in your url pattern, you have

url(r'u/dashboard/(?P<list_id>\d+)/$', views.user_dashboard, name='user_dashboard'),

需要 list_id 作为参数.

这篇关于Django-使用参数重定向视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆