0

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})

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'),

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.

which sites this:

return redirect('user_dashboard')

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}) 

What am I doing wrong here?

1 Answer 1

3

Try:

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

Your code

return redirect('user_dashboard')

would not work because in your url pattern, you have

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

which requires list_id as a parameter.

Sign up to request clarification or add additional context in comments.

6 Comments

I just tried that and it returned Reverse for 'user_dashboard' with arguments '()' and keyword arguments '{'args': (2,)}' not found.
oops.. :) i completely forgot the reverse :)
You shouldn't need to use redirect(reverse(...)) because redirect() accepts both args and kwargs and passes them to reverse. So: redirect('user_dashboard', list_id=2) should work.
With the reverse included it redirects. Now lets say I don't want to hardcode this with 2, and I want it to be the list_id for the given user. Do I simply create a local variable called lets say list_id and then pass that variable into args=()?
Yes. You can just replace 2 with list_id which is the local variable
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.