英文:
Crispy forms label inside of field
问题
使用Bootstrap 5、Django和Crispy Forms创建计算器应用程序。
应用程序按预期工作,但我想对表单的外观进行更改。
我已经通过简单地将以下内容添加到CSS文件中来移除了必填字段的星号:
.asteriskField {
display: none;
}
但现在我想将标签放在字段内部,这需要一种不同的方法,尽管我进行了广泛的研究,但目前无法理解。
使用Chrome中的检查工具,我可以看到标签当然是一个单独的元素,但是它被放在字段内部以实现所需的效果。
当前表单
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
Settings.py (excerpt)
CRISPY_TEMPLATE_PACK = 'bootstrap5'
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='State',
choices=[(zipcode, zipcode) for zipcode in StateTax.objects.values_list('zipcode', flat=True).distinct()]
)
Index.html (excerpt)
<div class="col-md-4 border bg-light">
<div class="row justify-content-center">
<h5 class="text-center"> Enter details below to see your estimate</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>
答案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("first_name"),
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论