Home > Back-end >  Django giving "Reverse for 'view' not found" error when view is defined
Django giving "Reverse for 'view' not found" error when view is defined

Time:05-05

While using django 4.0.4 I have encountered the error "Reverse for 'upload' not found. 'upload' is not a valid view function or pattern name". It was working earlier in the day, and I can't figure out the problem for the life of me. I'll include my views.py, urls.py, and the relevant section of my html file, all three of which are in the same folder of the project. If anyone has any advice I would greatly appreciate it.

Views.py

def welcome(request):
    return render(request, 'welcome.html')

def contact(request):
    return render(request, 'contact-us.html')

def how(request):
    return render(request, 'How-to-use.html')

def upload(request):
    if request.method == 'POST':
        if 'scatter_plot' in request.POST:
            form = UploadFileForm(request.POST.get, request.FILES)
            file=request.FILES['document']
            csv = CSV.objects.create(doc=file)
            os.system('python ../scatter_plot.py')
        if 'line_plot' in request.POST:
            form = UploadFileForm(request.POST.get, request.FILES)
            file=request.FILES['document']
            csv = CSV.objects.create(doc=file)
            os.system('python ../line_plot.py')
        return render(request, 'uploaded.html')
    else: 
        form = UploadFileForm

Urls.py

urlpatterns = [
    path('', views.welcome),
    path('admin/', admin.site.urls),
    path('contact-us/', views.contact),
    path('upload.html', views.upload),
    path('upload/', views.upload),
    path('welcome/', views.welcome),
    path('How-to-use/', views.how),
    path('contact-us.html', views.contact),
    path('welcome.html', views.welcome),
    path('How-to-use.html', views.how)
]

Welcome.html

<form method="POST" enctype="multipart/form-data" action="{% url 'upload' %}">
    {% csrf_token %}  
    <input type='file' name='document' accept='.csv'>
    <button type='submit' name='line_plot'>Graph as a line Graph</button>
    <button type='submit' name='scatter_plot'>Graph as a Scatter Plot</button>
</form>

CodePudding user response:

you need to put the name for the URL

path('upload/', views.upload, name='upload'),

Refer this

https://docs.djangoproject.com/en/4.0/topics/http/urls/#reversing-namespaced-urls https://docs.djangoproject.com/en/4.0/topics/http/urls/

  • Related