0

i don't use models form but only form. How to change location file when i uploaded? i want the file just go to /mp3 folder. And now it not move to anything (the file didn't got upload).

and this my code :

def homepage(request):
if request.method == "POST":
    form = Audio_store(request.POST, request.FILES)
    #  form = AudioForm(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")


def handle_uploaded_file(f):
    with open('mp3', 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

forms.py :

 from django import forms
    
    class Audio_store(forms.Form):
        record=forms.FileField()

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)

html :

<form method="POST" enctype="multipart/form-data">
                            {% csrf_token %}
                            {{form}}
                            <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>
                            </button>
                          </form>

and my error msg : cannot upload

my folder: folder

Monana
  • 43
  • 1
  • 5

1 Answers1

0

Hey As I saw your code and find that it is giving the reference before assignment error as of my experience in django it comes when it can't find the variable which we specify like form in your case and your code is not working because:-

  1. You are only handling the post request and then returning render method with some context

  2. Error is coming bcoz you are making GET request as you can see the request method as GET and it is also returning the render method but with some context which is form and django can't find the form so it is giving the error just handle GET request as well below is the code which is in my mind to solve the error:

code:

def homepage(request):
    if request.method == "POST":
        form = Audio_store(request.POST, request.FILES)
        #  form = AudioForm(request.POST, request.FILES)
        if form.is_valid():
             handle_uploaded_file(request.FILES['record'])
             return HttpResponseRedirect('mp3/')
        return render(request, "homepage.html", {'form': form})
     else:
          return render(request, "homepage.html")
Simar
  • 1
  • 1
  • it's not error again, but why my file not uploaded? the file just gone – Monana May 17 '22 at 07:08
  • had u added the enctype="multipart/form-data" in the form tag as attribute in html – Simar May 17 '22 at 15:32
  • already have it. i was update it again for you to check it – Monana May 17 '22 at 15:33
  • can you help me? i don't know where the file uploaded – Monana May 17 '22 at 16:13
  • check had u added the MEDIA_URL AND MEDIA_ROOT settings in settings.py file and one more thing add "upload_to" argument in filefield in models and upload_to takes the file directory name where you want to upload the file and it will create the folder itself of that name when file will be uploaded – Simar May 19 '22 at 20:25