django 数量表单小部件
我想开发一个基本数量小部件,它是一个下拉选择框,消耗一个整数,这将是最大数量,用户可以从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我已经完成了:
purchase_form(10)的输出:
也验证了:
Ok I have done it:
output for purchase_form(10):
also validates: