如何使用 Bootstrap 在 Django 中自定义复选框和单选框?

2023-12-14

我正在尝试使用 bootstrap 类自定义 Django 中的复选框和单选按钮。然而,这并没有奏效。

我已经尝试在 forms.py 中插入引导类widgets and attrs: custom-control custom-radio custom-control-inline custom-control-input custom-control-label

My code:

模型.py

Y = 1
N = 2

YN = (
    (Y, 'yes'),
    (N, 'no'),)

class Prop(models.Model):
    yesorno = models.IntegerField(choices=YN, default=Y, verbose_name='Label...')

forms.py

class PropForm(forms.ModelForm):
    class Meta:
        model = Prop
        exclude = ()
        widgets={
            'yesorno': forms.RadioSelect(
                attrs={
                    'class':'custom-control-input custom-control-label',
                }
            ),
        }

模板.html

<form action="" method="POST">
     {% csrf_token %}
     <div class="custom-control custom-radio custom-control-inline">
       {{ form.yesorno.label_tag }} {{ form.yesorno }}
     </div>
</form>

HTML 呈现

<div class="custom-control custom-radio custom-control-inline">
 <label for="id_yesorno_0">Label...?</label>
 <ul id="id_yesorno" class="custom-control-input custom-control-label">
   <li>
    <label for="id_yesorno_0"><input type="radio" name="yesorno" value="1" class="custom-control-input custom-control-label" required="" id="id_yesorno_0">yes</label>
   </li>
   <li>
    <label for="id_yesorno_1"><input type="radio" name="yesorno" value="2" class="custom-control-input custom-control-label" required="" id="id_yesorno_1" checked="">no</label>
   </li>
 </ul>
</div>

我想使用自定义引导样式(https://getbootstrap.com/docs/4.3/components/forms/#radios).

我需要渲染一个 HTML,如下面的代码所示:

<div class="custom-control custom-radio custom-control-inline">
  <input type="radio" id="customRadioInline1" name="customRadioInline1" class="custom-control-input">
  <label class="custom-control-label" for="customRadioInline1">Toggle this custom radio</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
  <input type="radio" id="customRadioInline2" name="customRadioInline1" class="custom-control-input">
  <label class="custom-control-label" for="customRadioInline2">Or toggle this other custom radio</label>
</div>

Expected result: custom checkbox and radio in Django using bootstrap


到目前为止我最好的解决方案。 我必须手动渲染。

模板.html

<!-- radio -->
<div class="form-group">
    {{ form.field_name.label_tag }}
    {% for pk, choice in form.field_name.field.widget.choices %}
    <div class="custom-control custom-radio custom-control-inline">
        <input id="id_{{form.field_name.name}}_{{ forloop.counter0 }}" name="{{form.field_name.name}}" type="{{form.field_name.field.widget.input_type}}" value="{{pk}}" class="custom-control-input"
         {% ifequal form.field_name.data pk.0 %}
           checked="checked"
         {% endifequal %}/>
        <label for="id_{{form.field_name.name}}_{{ forloop.counter0 }}" class="custom-control-label">{{ choice }}</label>
    </div>
    {% endfor %}
</div>

<!-- checkbox -->
<div class="form-group">
    {{ form.field_name.label_tag }}
    {% for pk, choice in form.field_name.field.widget.choices %}
    <div class="custom-control custom-checkbox custom-control-inline">
        <input id="id_{{form.field_name.name}}_{{ forloop.counter0 }}" name="{{form.field_name.name}}" type="{{form.field_name.field.widget.input_type}}" value="{{pk}}" class="custom-control-input"
         {% ifequal form.field_name.data pk.0 %}
           checked="checked"
         {% endifequal %}/>
        <label for="id_{{form.field_name.name}}_{{ forloop.counter0 }}" class="custom-control-label">{{ choice }}</label>
    </div>
    {% endfor %}
</div>

My result

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Bootstrap 在 Django 中自定义复选框和单选框? 的相关文章

随机推荐

  • 如何从 Tkinter 获取输入

    我正在使用 Tkinter 制作一个程序 其中用户输入以磅为单位的体重 然后输出以公斤为单位的体重 我在获取内容时遇到问题Entry来自用户 我正在计算磅换算为公斤clicked1 有人可以告诉我如何在那里获得条目输入吗 from Tkin
  • 为什么我保存的 D3 选择在某些情况下没有效果?

    我对如何保存 D3 选择供以后使用感到困惑 在下面的代码中 我的轴有一个 全局 变量 首次创建它们时我将它们保存到该变量中 后来 我可以将此变量用于某些事情 此处 设置一些文本 但不能使用其他变量 此处 更新比例 这仅在我显式 重新 选择轴
  • conda 更新 CondaHTTPError:HTTP 无

    跑步中途Conda Update all 更新停滞了 多个软件包已更新 现在 当我跑步时conda update all or conda update conda 我得到这样的回应 C Users AppData Local Contin
  • 使用 Indy 10 和 DELPHI 评估电子邮件

    我使用以下代码来评估消息 使用 INDY 10 组件收到的电子邮件消息的内容 正文 行 function LinesFromMsg aMsg TIdMessage TStrings var i Integer begin for i 0 t
  • 关于Java基本类型方法的问题

    我对 Java 中的原始类型以及将一种类型转换为另一种类型的方法感到困惑 比如说 如果我有一个整数并且想将其转换为字符串 我需要使用 Integer 或 String 的静态方法 例如 String valueOf some integer
  • Ansible 中的列表按自然字母数字顺序排序

    有没有办法以自然的方式对 Ansible 或 Jinja 中的列表进行排序 例如 这是列表 test test123 test12 test5 test1234test test22te 我需要它来考虑整个数字而不是单个数字 所以test1
  • 对于简单且可变的产品,以特定的 html 结构显示一些数据

    在 WooCommerce 中 我想创建一个函数 为每个变体或可变产品输出一个简单的数据 列表 或者 如果是一个简单的产品 那么该产品本身的细节 我需要为每个项目添加的详细信息是 regular price size attribute 用
  • 多个 BindingContext 在同一内容页上,两个不同的视图?

    我有一个 ContentPage 上面有两个 ContentView 我想将每个 ContentView 的绑定上下文设置为各自的 ViewModel 这是我对它们组合在一起的一个大型 ViewModel 的首选灵魂 MainPage
  • 如何在 JavaScript 中合并 TypedArray?

    我想合并多个数组缓冲区来创建一个 Blob 然而 如你所知 类型数组没有 推 或有用的方法 E g var a new Int8Array 1 2 3 var b new Int8Array 4 5 6 结果 我想得到 1 2 3 4 5
  • 重新安装 ruby​​gems

    如何重新安装 RubyGems 如中所述没有要加载的文件 rubygems LoadError 运行 Mac OS X 10 8 2 直接从源下载 我在尝试获取时遇到了很大的麻烦gem update system or gem instal
  • React Router Dom,useNavigate 未重定向到正确的 url 路径

    在较旧的 React Router Dom 版本中 如果用户登录 我可以使用此代码进行重定向 history push login redirect shipping Now in v6 我正在使用useNavigate函数而不是histo
  • 如何删除 MySQL 数据库中的重复行? (保留主 ID 最低的那个)

    假设我想首先选择 download link 相同的行 然后 我想保留具有最低主 ID 的那个 并丢弃其余的 有没有简单的 SQL 语句可以实现这一点 这行得通吗 delete from mytable where id not in se
  • 超出 LDAPException 大小限制

    我正在使用 unboundid ldap sdk 来执行 ldap 查询 我在运行 ldap 搜索查询时遇到一个奇怪的问题 当我对包含 50k 条目的组运行查询时 出现异常 我的例外 LDAPException resultCode 4 s
  • 脚本中的双连字符使 Firefox 渲染异常

    当我在纯 html 页面的 部分中有上述行时 Firefox 3 5 5 会将尾随 gt 呈现为文本 如果我将 c 更改为 c 则不会 有什么想法吗 由于经过处理的非常大的脚本 我的页面上出现了一个工件 我可以将语句更改为 c 1 并暂时避
  • AS3:获取 Matrix 对象的比例

    最常见的问题是如何缩放 DisplayObject 答案通常是使用 Matrix 我的问题是 如何获取矩阵的比例 scaleX 和scaleY 有一个 Matrix scale 方法来设置scaleX和scaleY 但它不返回值 并且不存在
  • 如何以编程方式确定 iOS 中的默认电子邮件帐户/地址?

    在 iOS 设备上 一次可以配置多个电子邮件帐户 在 设置 应用中 用户可以选择默认电子邮件帐户 邮件 gt 邮件 默认帐户 此默认帐户是通过 MFMailComposeViewController 发送电子邮件时的 发件人 地址 是否有
  • Python 中 dir() 和 __dict__ 最大的区别是什么

    class C object def f self print self dict print dir self c C c f output class delattr f 为什么 self dict 中没有 f dir 所做的不仅仅是仰
  • 将 JPanel 变成 JOptionPane.OK_OPTION

    目前我有一个扩展 JPanel 的类 基本上显示有关传递到其构造函数的对象的一些信息 屏幕上有各种标签和图像图标 并且有一个 BorderLayout 设置 当用户左键单击主 GUI 中的 ImageIcon 并显示在屏幕上时 会触发此面板
  • .NET 中集合的内存分配

    这可能是一个骗局 我没有找到足够的信息 我正在讨论 Net 中集合的内存分配 集合中分配的元素的内存在哪里 List
  • 如何使用 Bootstrap 在 Django 中自定义复选框和单选框?

    我正在尝试使用 bootstrap 类自定义 Django 中的复选框和单选按钮 然而 这并没有奏效 我已经尝试在 forms py 中插入引导类widgets and attrs custom control custom radio c