Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django redirect after form submission not working

new to django

so this one probably has a very simple answer but i cannot for the life of me find the specific solution to this. I am simply trying to redirect to a new URL after a form submission with a FileField.

I can navigate to the URL separately and it works fine.

The file uploads correctly so I know it is validated correctly.

But the redirect returns the following error:

Reverse for 'success' not found. 'success' is not a valid view function or pattern name.

I have tried a bunch of different naming conventions, but none has worked. It looks to me like I have setup the URL and passed it correctly.

Would really appreciate some help with this. The simplest problems are the most frustrating!

Here are the views.

from django.shortcuts import render, redirect
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from .forms import InvestmentReportForm

def upload(request):
    if request.method == 'POST':
        form = InvestmentReportForm(request.POST, request.FILES)

        if form.is_valid():
            form.save()

            return redirect('success')
    else:
        form = InvestmentReportForm()
    return render(request, 'app/upload.html', {'form': form})

def success(request):
    return HttpResponse("File successfully uploaded")

And my urls.py:

app_name = 'app'
urlpatterns = [
                path('', views.index, name='index'),
                path('upload/', views.upload, name='upload'),
                path('success/', views.success, name='success'),
                path('performance/', views.performance, name='performance'),
                ]
like image 806
Ryan Skene Avatar asked Oct 29 '25 14:10

Ryan Skene


1 Answers

The answer was simple as I suspected. For others, if you use a namespace for a set of url patterns, you have to refer to that namespace when calling those urls. For this example:

return redirect('app:success')

like image 130
Ryan Skene Avatar answered Oct 31 '25 05:10

Ryan Skene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!