首页 > 解决方案 > 不了解 Django 表单并使用,有没有办法忽略它们并仍然保存表单中上传的文件?

问题描述

我正在尝试使用我不熟悉的 django 创建一个网站。不知道什么 django 表单我纯粹用 html 编写了我的 html 文件,现在我很难保存上传的文件,因为 django 不会让我手动保存它们。我试图绕过这个没有运气,所以我认为现在我必须在 django html 模板中重写我的 html 文件,这样我才能保存文件。我有一个这样的html文件:

html

<div class="w3-container w3-white">
      <select class="w3-select" name="request_function" id="request_function" >
                 <option id="faces_embedding" value="faces_embedding" style="" onclick="pagechange()">faces_embedding</option>
                <!-- <option id="faces_model" value="faces_model" style="" onclick="pagechange()"> faces_model </option> -->
                 <option id="image_recognizer"value="image_recognizer" style="" onclick="pagechange()">image_recognizer</option>
                 <option id="video_recognizer" value="video_recognizer" style="" onclick="pagechange()">video_recognizer</option>
                 <option id="stream_recognition" value="stream_recognition" style="" onclick="pagechange()">stream_recognizer</option>
                 <option id="help" value="help" style="" onclick="pagechange()">help</option>
      </select>
    </div>

<div id="AddFaces" style="visibility: visible; display: block;">
      <div class="w3-row-padding" style="margin:0 -16px;" >

            <div class="w3-half">
              <input type="radio" name="input_method" id="input_method_0" onclick="datachange()" checked value="input_method_0">
              <label>Add A Database</label>
            </div>
            <div class="w3-half">
              <input type="radio" name="input_method" id=input_method_1" onclick="datachange()" value="input_method_1">
              <label>Add A Face</label>
            </div>
          </div>

          <div class="w3-row-padding" style="margin:0px -10px;">
            <div id="dataset" class="w3-half w3-margin-bottom" style="visibility: visible; display: block;">
              <label>Dataset Path</label>
              <input class="w3-input w3-border" type="text" placeholder="directory path" name="dataset_path" id="dataset_path" required >
            </div>

            <div id="face" style="visibility: hidden; display: none;">
            <div  class="w3-half w3-margin-bottom">
              <label>Images Path</label>
              <input type="file" id="face_files" name="face_files" multiple >
            </div>

            <div class="w3-half">
              <label>Name</label>
            <input class="w3-input w3-border" type="text" placeholder="person name" name="face_name" id="face_name" >
            </div>
            </div>
          </div>
      </div>

其中“datachange()”是一个 javascript 函数,它隐藏了我不需要的不同潜水和我的 view.py

    if request.method == "GET":
        return render(request, 'index.html')
    if request.method == "POST":
        form = InputForm(request)
        call_form_function(form)
        return render(request, 'index.html')

其中“InputForm.py”是一个普通的python类,它接受表单id标签并将它们用作函数的输入。

   def __init__(self, request):
        self.request_function = request.POST.get("request_function")

        self.input_method = request.POST.get('input_method')

        self.dataset_path = request.POST.get('dataset_path')

        self.face_files = request.FILES.getlist['face_files']

        self.face_name = request.POST.get('face_name')

django 文档中,我使用了这个函数:

def handle_uploaded_file(f):
    with open('some/file/name.txt', 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)

但是在我写任何东西之前,django 会自动关闭文件。

问题:所以我的问题是我必须使用 django froms 来保存上传文件吗?有没有办法绕过这个?如果没有办法这样做,我怎么能以在网页中看起来仍然相同的方式更改我的 html 文件,并且我的 javascripts 可以与 django 模板一起使用。ps:我也没有数据库models.py,因为我对它们没有用处。

标签: pythonhtmldjangodjango-formsdjango-templates

解决方案


好吧,如果你不使用表单也不使用数据库,那你为什么要使用 Django :) 与所有使用 MVC(模式、视图、控制器)或在 Django 案例中的 MTV(模型、模板、视图)的现代 Web 框架一样) 你迟早会需要这三个。

模型定义了你的数据库模式,模板定义了 html 模板——用户将看到什么,而视图实际上是编程逻辑所在。

如果模板有表单,则需要在 Django 中定义表单。它实际上大大简化了生活,如果您使用模型,它可以是非常简单的定义。并且有人可以上传的有关您的文件(元数据)的信息应存储在模型(数据库)中。这将大大简化您对文件的处理。

但最终这真的取决于您的应用程序在做什么?


推荐阅读