【问题标题】:How to add crispy forms to bootstrap4 in Django?如何在 Django 中向 bootstrap4 添加松脆的表单?
【发布时间】:2022-01-05 10:53:55
【问题描述】:

我在将引导上传表单与 django 表单(脆表单)结合起来时遇到了麻烦。知道如何将酥脆的表单放入引导表单。

这是 django 脆皮表格

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

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

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

【问题讨论】:

  • 你的 forms.py 是什么样的?

标签: python html django bootstrap-4 django-crispy-forms


【解决方案1】:

你可以这样做

        <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']

【讨论】:

  • 完美,非常感谢!
猜你喜欢
  • 2022-01-04
  • 2014-12-17
  • 2023-03-11
  • 1970-01-01
  • 2013-07-01
  • 2020-08-21
  • 2014-11-13
  • 1970-01-01
  • 2013-02-15
相关资源
最近更新 更多