html - 如何将 'Choose File' 输入的 Bootstrap 4 样式应用于 Django 表单?

标签 html twitter-bootstrap django-templates bootstrap-4

我正在显示一个涉及在我的 Django 模板中上传图像的表单。

这是模板:

{% extends 'header.html' %}

{% block content %}
<br>
<div class="w-50 card mx-auto">
    <div class="card-body text-center">
        <h2 class="card-title">New Post</h2>
        <form method="post">
            {% csrf_token %}
            <p><strong>
                Title<br>
                {{ form.Title }}
            </strong></p>
            <p><strong>
                Description<br>
                {{ form.Description }}
            </strong></p>
            <p><strong>
                Image 
                {{ form.Image }}
            </strong></p>
            <p><strong>
                Election<br>
                {{ election_form.PostElection }}
            </strong></p>
            <button type="submit" class='button btn btn-outline-dark'>Post</button>
        </form>
    </div>
</div>

{% endblock %}

如您所见,表单上的提交按钮附加了一些 Bootstrap。这使得它看起来与图像上传表单上的“选择文件”按钮不同。如何将应用到提交按钮的 Bootstrap 应用到表单中的按钮?

最佳答案

爬取互联网后,我找到了下面的链接,但首先是瘦身:

使用 modelForm 在选择文件按钮上实现 bootstrap4 样式

***forms.py***
class ProfileForm(forms.ModelForm):
class Meta:
    model = Profile
    fields = ('avatar',)
    widgets = {'avatar':forms.FileInput(
        attrs={'style':'display: none;','class':'form-control', 'required': False, } 
         )}


****update_form.html****
**call each feil individualy**
<label class="btn btn-outline-secondary btn-lg">
    *button text goes here*
  {{profile_form.avatar}}
</label>

要点是在 HTML 中使用带有 Bootstrap 类的跨度。 然后在传入“样式”属性(CSS)“显示:无;”的同时将您的 form.field 嵌套在里面

https://www.abeautifulsite.net/whipping-file-inputs-into-shape-with-bootstrap-3

关于html - 如何将 'Choose File' 输入的 Bootstrap 4 样式应用于 Django 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48750955/

相关文章:

html - 如何: CSS3 animated Popup bubble and text Fade in effect with Angularjs

image - 在 div 内拉伸(stretch)背景并使其可滚动

html - Bootstrap 右对齐列

python - 变量为无时默认模板标签输出的Django设置?

PHP 自助服务器不工作

javascript - HTML/CSS 元素在滚动时消失。

css - 负上边距+溢出:hidden on Boostrap carousel

django - 使用带有特殊字符的 Django 的 url 模板标签

python - Django 使用 crispy 表单用 css 渲染表单字段

javascript - 将 Font Awesome 图标设置为光标 - 这可能吗?