'how to change folder location to save file - DJANGO FileSystemStorage

so i want when i upload a file, the file will go to media/mp3 not media/txt. how to change save file location?

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 HttpResponseRedirect('mp3/')
return render(request, "homepage.html", {'form': form})

folder

error

error



Solution 1:[1]

You can specify FileSystemStorage.location

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'])
          return HttpResponseRedirect('mp3/')
    return render(request, "homepage.html", {'form': form})

To use this you've to set MEDIA_ROOT inside your settings.py file because FileSystemStorage.location uses MEDIA_ROOT as default value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1