如何在 Django 中向 bootstrap4 添加脆皮形式?

How to add crispy forms to bootstrap4 in Django?

我在将 bootstrap 上传表单与 Django 表单(脆皮表单)结合起来时遇到了麻烦。知道如何将脆皮表格变成 bootstrap 表格。

这是 django 脆皮形式

        <form method="post" enctype="multipart/form-data">
              {% csrf_token %}
              {{ form|crispy }}
              <button type="submit" class="btn btn-primary">Upload and Download!</button>
        </form>

这是bootstrap表格,我想放脆皮表格。

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="custom-file">
  <input type="file" class="custom-file-input" id="customFile">
  <label class="custom-file-label" for="customFile">Choose file</label>
</div>

知道如何将这两者结合起来吗?

你可以这样做

        <form action="." method='POST' enctype="multipart/form-data">
        {% csrf_token %}
    <div class="custom-file">
  <input type="file" class="custom-file-input" name="file" id="customFile">
  <label class="custom-file-label" for="customFile">Choose file</label>
</div> <br><br>
    <button type="submit" class="btn btn-primary">Upload and Download!</button>
    </form>

并将其添加到页面的头部

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

并在您的 views.py 中获取您可以使用的文件 files = request.Files['file']