Django 在定义视图时给出 "Reverse for 'view' not found" 错误

Django giving "Reverse for 'view' not found" error when view is defined

在使用 django 4.0.4 时,我遇到错误“找不到 'upload' 的反向。'upload' 不是有效的视图函数或模式名称”。它在当天早些时候工作,我无法解决我一生的问题。我将包括我的 views.py、urls.py 和我的 html 文件的相关部分,所有这三个都位于项目的同一文件夹中。如果有人有任何建议,我将不胜感激。

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>

您需要输入 URL

的名称
path('upload/', views.upload, name='upload'),

参考这个

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