Crispy forms label inside of field

huangapple go评论51阅读模式
英文:

Crispy forms label inside of field

问题

使用Bootstrap 5、Django和Crispy Forms创建计算器应用程序。
应用程序按预期工作,但我想对表单的外观进行更改。

我已经通过简单地将以下内容添加到CSS文件中来移除了必填字段的星号:

.asteriskField {
  display: none;
}

但现在我想将标签放在字段内部,这需要一种不同的方法,尽管我进行了广泛的研究,但目前无法理解。
使用Chrome中的检查工具,我可以看到标签当然是一个单独的元素,但是它被放在字段内部以实现所需的效果。

当前表单

Crispy forms label inside of field

期望的结果
Crispy forms label inside of field
Crispy forms label inside of field

Settings.py(摘录)

CRISPY_TEMPLATE_PACK = 'bootstrap5'

Forms.py(摘录)

class InvestmentForm(forms.Form):
    starting_amount = forms.ChoiceField(choices=STARTING_AMOUNT)
    deposit_amount = forms.FloatField()
    trade_in_value = forms.FloatField()
    number_of_years = forms.FloatField()
    credit_score = forms.ChoiceField(choices=CREDIT_SCORE)
    state = forms.ChoiceField(
        label='State',
        choices=[(zipcode, zipcode) for zipcode in StateTax.objects.values_list('zipcode', flat=True).distinct()]
    )

Index.html(摘录)

<div class="col-md-4 border bg-light">

    <div class="row justify-content-center">
        <h5 class="text-center"> 在下面输入详细信息以查看您的估算</h5>
    </div>
    <div class="col-md-8 mx-auto">
        <form action="" method="POST">
            {% csrf_token %}
            {{ form|crispy }}
            <br>
            <button class="btn btn-primary" type="submit">Calculate</button>                
        </form>
        <br>
    </div>

</div>
英文:

Using Bootstrap5, Django and Crispy Forms to create a calculator application.
Application works as expected however I want to make changes to the appearance of the forms.

I've already removed the required field asterisk by simply adding the following to the CSS file:

.asteriskField {
  display: none;
}

But now I want to place the label inside the field which requires a different approach that I'm currently unable to understand despite extensive research.
Having used Inspect in Chrome I can see that the label is of course a separate element but one housed within the field to achieve the given effect.

Current Form

Crispy forms label inside of field

Desired result
Crispy forms label inside of field

Crispy forms label inside of field

Settings.py (excerpt)

CRISPY_TEMPLATE_PACK = &#39;bootstrap5&#39;

Forms.py (excerpt)

class InvestmentForm(forms.Form):
    starting_amount = forms.ChoiceField(choices=STARTING_AMOUNT)
    deposit_amount = forms.FloatField()
    trade_in_value = forms.FloatField()
    number_of_years = forms.FloatField()
    credit_score = forms.ChoiceField(choices=CREDIT_SCORE)
    state = forms.ChoiceField(
        label=&#39;State&#39;,
        choices=[(zipcode, zipcode) for zipcode in StateTax.objects.values_list(&#39;zipcode&#39;, flat=True).distinct()]
    ) 

Index.html (excerpt)

<div class="col-md-4 border bg-light">

&lt;div class=&quot;row justify-content-center&quot;&gt;
        &lt;h5 class=&quot;text-center&quot;&gt; Enter details below to see your estimate&lt;/h5&gt;
        &lt;/div&gt;
        &lt;div class=&quot;col-md-8 mx-auto&quot;&gt;
    &lt;form action=&quot;&quot; method=&quot;POST&quot;&gt;
        {% csrf_token %}
        {{ form|crispy }}
        &lt;br&gt;
        &lt;button class=&quot;btn btn-primary&quot; type=&quot;submit&quot;&gt;Calculate&lt;/button&gt;                
    &lt;/form&gt;
    &lt;br&gt;
&lt;/div&gt;

&lt;/div&gt;

答案1

得分: 1

你可以使用 crispy-bootstrap5,它引入了 浮动标签。只需按照安装指南,并在你的 crispy 布局中添加一个浮动输入字段,如下所示:

FloatingField("first_name"),
英文:

You could use crispy-bootstrap5 which introduces floating labels. Just follow the installation guidelines and add a floating input field in your crispy Layout like:

FloatingField(&quot;first_name&quot;),

huangapple
  • 本文由 发表于 2023年2月14日 04:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定