Redirect after post causes an error

I have the following code to save an entry (section) from a form. The save works fine and after the save I want to redirect the user back to a form showing a list of sections. Here is the code.

def album_section_new(request, album_pk):
    form = AlbumSectionForm()
    source = request.session['album_source']
    if request.method == 'POST':
        form = AlbumSectionForm(request.POST, request.FILES)
        if form.is_valid():
            this_album = Album.objects.get(pk=album_pk)
            album_section = form.save(commit=False)
            album_section.album_id = this_album
            album_section.save()
            context = {"pk": album_pk,
                       "source": source,}
            return redirect('album_sections.html', context=context)
    else:
        context = {"form": form,
                   "album_pk": album_pk,
                   "source": source}
        return render(request, 'Albums/album_section_edit.html',
                      context)

The return redirect(‘album_sections.html’, context-context) should presumably take the user to that page with the PK of the album and the list will show the newly added section. However, I’m getting a 404 error message and the final line in the error shows this.

The current path, albums/album_section_new/f74cf79c-0dc2-4981-a34b-c174d4e2d6b0/album_sections.html , didn’t match any of these.

I’m not sure why through the redirect it is prefixing the url with the current url.

Any ideas on what I’m missing here?

Thanks.

You use redirect with a URL, not a template to be rendered.

What is your URL for the view that renders album_sections.html? That’s what you want to redirect to.

This is the url in urls.py…

path('album-sections/<str:pk>/<str:source>/', views.album_sections, name='album_sections')

Okay, thanks Ken, you pushed me in the right direction and got me thinking correctly about this.

I changed the code to…

return redirect('album_sections', pk=album_pk, source=source)

and this worked.

Thanks!
Bob