Django:将$添加到表单字段的简单解决方案
Django: Simple solution to prepend $ to form field
我想在我的一些表单的输入字段前加上一个 $。我不希望将 $ 作为输入值的一部分发送到后端。我发现了一些堆栈溢出问题,这些问题提示了以下更新:
self.fields['buy_price'].localize = True
self.fields['buy_price'].widget.is_localized = True
self.fields['buy_price'].prefix = '$'
但其中 none 对我有用。我也希望避免添加 50 多行代码,例如 S.O 中推荐的那样。答案:How to represent Django money field with currency symbol in list template and plain decimal for edits?
HTML形式:
{% url 'update-buy/' offer.id as buy_update_url %}
<form method="POST" action="{{ buy_update_url }}">
<div class="form-group">
<legend class="border-bottom mb-4">Update Offer to Buy a Game</legend>
{% csrf_token %}
{{ form|crispy }}
{{ form.media }}
</div>
<input type="hidden" id="hidden_desired_game_id" name="hidden_desired_game" value="{{ offer.desired_game_id }}">
<div class="form-group">
<button onclick="clearMessage()" class="post-button btn btn-outline-info" type="submit">Update</button>
</div>
</form>
有人有简单的方法吗?
更新:
我更新为使用 {% crispy form %}
而不是 {{ form|crispy }},这很好地显示了 $.但是现在,带有 id="hidden_desired_game_id"
的隐藏输入不包含在 POST 请求中。出于某种原因,在呈现表单时(下面的屏幕截图),该输入现在位于表单下方,而不是表单内部。 知道我怎么还能把它包括在内吗?
编辑 #2:我通过将输入字段移到表单中更高的位置来解决上述问题。但现在看起来 jquery 正在加载两次或其他内容。 Desired game 字段右侧有 2 个下拉箭头,看起来很难看。我尝试使用 javascript 来操纵 class 并使用 crispy-forms 中的 css_class
功能,但我无法让它只有一个下拉菜单。有谁知道如何解决这个问题?截图如下
已找到解决方案:最终更新:
我可以通过添加此 javascript:
来解决上述 2 个下拉列表的问题
window.onload = function () {
$( ".custom-select" ).removeClass("custom-select"); // Remove dupe dropdown
}
有点老套但是哦好吧!现在一切都很好
forms.py
from crispy_forms.helper import FormHelper
from crispy_forms.bootstrap import PrependedText
class ProductForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
PrependedText('buy_price', '$')
)
而不是 {{ form|crispy }}
你必须使用 {% crispy form %}
表单域看起来像
我想在我的一些表单的输入字段前加上一个 $。我不希望将 $ 作为输入值的一部分发送到后端。我发现了一些堆栈溢出问题,这些问题提示了以下更新:
self.fields['buy_price'].localize = True
self.fields['buy_price'].widget.is_localized = True
self.fields['buy_price'].prefix = '$'
但其中 none 对我有用。我也希望避免添加 50 多行代码,例如 S.O 中推荐的那样。答案:How to represent Django money field with currency symbol in list template and plain decimal for edits?
HTML形式:
{% url 'update-buy/' offer.id as buy_update_url %}
<form method="POST" action="{{ buy_update_url }}">
<div class="form-group">
<legend class="border-bottom mb-4">Update Offer to Buy a Game</legend>
{% csrf_token %}
{{ form|crispy }}
{{ form.media }}
</div>
<input type="hidden" id="hidden_desired_game_id" name="hidden_desired_game" value="{{ offer.desired_game_id }}">
<div class="form-group">
<button onclick="clearMessage()" class="post-button btn btn-outline-info" type="submit">Update</button>
</div>
</form>
有人有简单的方法吗?
更新:
我更新为使用 {% crispy form %}
而不是 {{ form|crispy }},这很好地显示了 $.但是现在,带有 id="hidden_desired_game_id"
的隐藏输入不包含在 POST 请求中。出于某种原因,在呈现表单时(下面的屏幕截图),该输入现在位于表单下方,而不是表单内部。 知道我怎么还能把它包括在内吗?
编辑 #2:我通过将输入字段移到表单中更高的位置来解决上述问题。但现在看起来 jquery 正在加载两次或其他内容。 Desired game 字段右侧有 2 个下拉箭头,看起来很难看。我尝试使用 javascript 来操纵 class 并使用 crispy-forms 中的 css_class
功能,但我无法让它只有一个下拉菜单。有谁知道如何解决这个问题?截图如下
已找到解决方案:最终更新:
我可以通过添加此 javascript:
来解决上述 2 个下拉列表的问题window.onload = function () {
$( ".custom-select" ).removeClass("custom-select"); // Remove dupe dropdown
}
有点老套但是哦好吧!现在一切都很好
from crispy_forms.helper import FormHelper
from crispy_forms.bootstrap import PrependedText
class ProductForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.layout = Layout(
PrependedText('buy_price', '$')
)
而不是 {{ form|crispy }}
你必须使用 {% crispy form %}
表单域看起来像