Django как загружать файлы в разные директории на основе опции выпадающего списка из шаблонов

Я новичок в Django, я хочу сохранять загруженные файлы в каталогах на основе значений выпадающего списка из шаблонов, и мой код выглядит следующим образом,

1:models.py:

def user_directory_path(request,*args,**kwargs):
    need_selection = request.POST.get('need_selection')
    return need_selection


class Document(models.Model):
        docfile = models.FileField(upload_to=user_directory_path)

2:home.html:

<div class="row" style="top:0;margin-left:50px;right:0;">
  <div class="column" style="width:30%;margin-left:200px;border:2px solid red;margin-top:40px;">
    
    <br>

    <p style="font-size:30px;color:red;margin-left:200px">please uploads files</p>
    <br>
    <form method="POST" id="fileupload" enctype="multipart/form-data" style="font-size:30px">
    {% csrf_token %}
      please choose your requirement:
    <select form='fileupload' name="need_selection" id="need-selection" style="font-size:20px" required>
      {% for request_item in request_lst %}
      <option>{{request_item}}</option>
      {% endfor %}
    </select>
      <br>
      <br>
    <input form='fileupload' type="file" name="docfile" style="font-size:30px;color:red">
      <br>
      <br>
    <button type="submit" style="font-size:30px;margin-left:0px">Upload</button>
      <br>
      <br>
      <br>
  </form>

</div>

3:views.py

def home(request):
    if request.method == 'POST':

        newdoc = Document(docfile=request.FILES['docfile'])
        newdoc.save()

        # Redirect to the document list after POST
        return redirect('home')
    request_lst = ['request1','request2','request3','request4']


    context = {
                'request_lst': request_lst
    }

    return render(request, 'myapp/templates/home.html',context=context)

но, это не работает. кто-нибудь может мне помочь? спасибо.

Вернуться на верх