[Answered ]-Django not displaying correct URL after reverse

2👍

As you correctly mentioned you will never redirect to foo() from foo().

So the simple way to fix this is just add similar code as in main_page() view:

def foo(request, grade_level):

    if request.method == 'POST':
        grade_level = request.POST['grade_picked']
        return HttpResponseRedirect(reverse('foo', args = (grade_level,)))
    else:
        parent_list = # get stuff from database
        emails      = # get stuff from database

        return render(request, 'list.html', context = {'grade_list' : grade_list, 'parent_list' : parent_list})

Please note that I remove grade_level = request.POST['grade_picked'] because as Nagkumar Arkalgud correctly said it is excessively.

Also instead of combination of HttpResponseRedirect and reverse you can use shortcut redirect which probably little easy to code:

from django.shortcuts redirect
...
return redirect('foo', grade_level=grade_level) 

0👍

I would suggest you to use kwargs instead of args.
The right way to use the view is:

your_url = reverse("<view_name>", kwargs={"<key>": "<value>"})

Ex:

return HttpResponseRedirect(reverse('foo', kwargs={"grade_level": grade_level}))

Also, you are sending “grade_level” to your view foo using the URL and not a POST value. I would remove the line:

grade_level = request.POST['grade_picked']

as you will override the grade_level sent to the method from the url.

Leave a comment