表单未出现 - UnboundLocalError at / DJANGO FORM

form doesn't appear - UnboundLocalError at / DJANGO FORM

我的表格没有出现在我的网站上,现在出现错误。这是我的错误信息:

UnboundLocalError at / local variable 'form' referenced before assignment Request Method: GET Request URL: http://localhost:8000/ Django Version: 3.0.14 Exception Type: UnboundLocalError Exception Value: local variable 'form' referenced before assignment Exception Location: F:\KULIAH\SEMESTER8\SKRIPSI\MusicLockApp\MusicLockApp\views.py in homepage, line 20 Python Executable: C:\Users\Capoo\python.exe Python Version: 3.10.2 Python Path: ['F:\KULIAH\SEMESTER8\SKRIPSI\MusicLockApp', 'C:\Users\Capoo\python310.zip', 'C:\Users\Capoo\DLLs', 'C:\Users\Capoo\lib', 'C:\Users\Capoo', 'C:\Users\Capoo\lib\site-packages'] Server time: Thu, 19 May 2022 06:33:52 +0000

这是我的 views.py :

def homepage(request):
    if request.method == "POST":
        form = Audio_store(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['record'])
            return render(request, "homepage.html", {'form': form})
    else:
        return render(request, "homepage.html", {'form': form})
    return render(request, "homepage.html")

html :

<form method="POST" enctype="multipart/form-data">
                            <button type="submit" class="dsnupload">
                                <i class="large material-icons" style="font-size: 50pt; margin-top: 10px;">audiotrack</i>
                                <p style="font-weight: bold; color: white;">Insert file audio (mp3)</p>
                                {% csrf_token %}
                                {{form}}
                            </button>
                          </form>

form.py :

from django import forms

class Audio_store(forms.Form):
    record=forms.FileField(widget=forms.FileInput(attrs={'style': 'width: 300px;', 'class': 'form-control'}))

urls.py :

urlpatterns = [
     url(r'^admin/', admin.site.urls),
     url(r'^decode/$', views.decode),
     path("", views.homepage, name="upload")
 ]

if settings.DEBUG: #add this
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

我想让我的表格立即出现,请帮助我

试试这个

def homepage(request):
    if request.method == "POST":
        form = Audio_store(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['record'])
            return render(request, "homepage.html", {'form': form})
    else:
        return render(request, "homepage.html", {'form': Audio_store()})
    return render(request, "homepage.html")

例子 https://docs.djangoproject.com/en/4.0/topics/forms/#the-view

就这样,表格会立即出现。

   def homepage(request):
        form_class = Audio_store
        form = form_class(request.POST or None)
        if request.method == "POST":
            form = Audio_store(request.POST, request.FILES)
            if form.is_valid():
                handle_uploaded_file(request.FILES['record'])
                return render(request, "homepage.html", {'form': form})
        return render(request, "homepage.html", {'form': form})

当您单击 URL 时,将执行 GET 请求。根据您的代码,当请求进入其他部分时,请求假设显示一个空表单,但您尚未定义表单 class 对象并将此对象作为上下文传递给 return 语句,这就是您得到的原因这个错误。

试试这个代码:

def homepage(request):
    form = Audio_store()
    if request.method == "POST":
        form = Audio_store(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['record'])
            form.save()
            return render(request, "homepage.html", {'form': form})
    return render(request, "homepage.html", {'form': form})

希望对你有用。