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?

Chris Clouten
  • 1,075
  • 3
  • 11
  • 24

1 Answers1

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.

karthikr
  • 97,368
  • 26
  • 197
  • 188
  • 1
    I just tried that and it returned Reverse for 'user_dashboard' with arguments '()' and keyword arguments '{'args': (2,)}' not found. – Chris Clouten May 31 '13 at 20:52
  • oops.. :) i completely forgot the `reverse` :) – karthikr May 31 '13 at 20:54
  • You shouldn't need to use `redirect(reverse(...))` because [`redirect()`](https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#redirect) accepts both `args` and `kwargs` and passes them to `reverse`. So: `redirect('user_dashboard', list_id=2)` should work. – Ngenator May 31 '13 at 21:06
  • 1
    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=()`? – Chris Clouten May 31 '13 at 21:12
  • Yes. You can just replace 2 with list_id which is the local variable – karthikr May 31 '13 at 21:15
  • Sure, you should be able to do something like `redirect('user_dashboard', YOUR_VARIABLE)` or `redirect('user_dashboard', list_id=YOUR_VARIABLE)` since it accepts `*args` and `**kwargs` – Ngenator May 31 '13 at 21:15