Home > Mobile >  form doesn't appear - UnboundLocalError at / DJANGO FORM
form doesn't appear - UnboundLocalError at / DJANGO FORM

Time:05-20

my form doesn't appear at my website and its error now. this is my error message :

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

error

here's my 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" >
                                <i  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)

i want my form appear instanly, please help me

CodePudding user response:

try this

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")

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

CodePudding user response:

just like this and the form will appear instanly.

   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})
  • Related