django 数量表单小部件

发布于 2024-08-22 04:17:44 字数 500 浏览 10 评论 0原文

我想开发一个基本数量小部件,它是一个下拉选择框,消耗一个整数,这将是最大数量,用户可以从 1 到最大数量进行选择。

最后我的表单将使用这个小部件,如果给定的金额大于最大值,则它不应该验证。 (事实上​​,普通用户无法选择超过最大值的选项,但我想可以通过向服务器发送直接请求来尝试。)

这是如何做到的?

谢谢

编辑: 我认为一开始可能是这样的,但是我希望我的字段是一个选择(从 1 到最大数量),而不是文本输入字段。

def quantity_field(quantity=1):
    class QuantityForm(forms.Form):
        forms.IntegerField(label="Purchase quantity",min_value=1,max_value=quantity,required=True,widget=forms.Select)
    return QuantityForm

I want to develop a basic quantity widget that is a dropdown selection box, consuming an integer which will be the maximum amount of quantity, users can select from 1 to the maximum quantity.

And in the end my form will be using this widget and if somehow the given amount is greater than the maximum, it shouldn't validate. (indeed regular users won't be able to select more than maximum but I guess it can be tried by sending direct request to the server.)

How can this be done?

Thanks

edit:
I think it can be something like this to begin with, however I want my field to be a select(from 1 to max maximum quantity), not textinput field.

def quantity_field(quantity=1):
    class QuantityForm(forms.Form):
        forms.IntegerField(label="Purchase quantity",min_value=1,max_value=quantity,required=True,widget=forms.Select)
    return QuantityForm

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2024-08-29 04:17:44

好的,我已经完成了:

def purchase_form(quantity=1):
    class QuantityForm(forms.Form):
        forms.IntegerField(label="Purchase quantity",min_value=1,max_value=quantity,required=True,widget=forms.Select(choices=  [ (i,i) for i in range(1,quantity+1) ]) )
    return QuantityForm

purchase_form(10)的输出:

>>>print d
<tr><th>Purchase quantity:</th><td><select name="x">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></td></tr>

也验证了:

>>> d.clean(5)
5
>>> d.clean(11)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/forms/fields.py", line 187, in clean
    raise ValidationError(self.error_messages['max_value'] % self.max_value)
ValidationError: [u'Ensure this value is less than or equal to 10.']

Ok I have done it:

def purchase_form(quantity=1):
    class QuantityForm(forms.Form):
        forms.IntegerField(label="Purchase quantity",min_value=1,max_value=quantity,required=True,widget=forms.Select(choices=  [ (i,i) for i in range(1,quantity+1) ]) )
    return QuantityForm

output for purchase_form(10):

>>>print d
<tr><th>Purchase quantity:</th><td><select name="x">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select></td></tr>

also validates:

>>> d.clean(5)
5
>>> d.clean(11)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.6/dist-packages/django/forms/fields.py", line 187, in clean
    raise ValidationError(self.error_messages['max_value'] % self.max_value)
ValidationError: [u'Ensure this value is less than or equal to 10.']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文