哪个Django表单字段可以向我提供< input type ="range"/> ;? [英] Which Django Form Field can provide me a HTML output of <input type="range" />?
本文介绍了哪个Django表单字段可以向我提供< input type ="range"/> ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我输入了一个html表单:
I've got a html form with this input:
<input id="duration_slider" type="range" min="1" max="168" value="48" />
但是我现在想将此滑块转换为Django表单字段,因此我可以在Django表单中使用它.字段名称是 duration
,所以我将 id
更改为 id = id_duration
.
However I'd now like to convert this slider to a django form field, so I can use it in a Django form. The field name is duration
so I'll change the id
to id=id_duration
.
此字段的我的models.py和forms.py会是什么样子?
What would my models.py and forms.py looks like for this field?
当前只是:
class AdvertisePost(Post):
...
duration = models.IntegerField(default=48)
def __str__(self):
return self.title
class AdvertisePostForm(forms.ModelForm):
duration = ?
class Meta:
model = AdvertisePost
fields = [
...
#'duration'
]
编辑
观看次数
if options_form.is_valid():
instance = options_form.save(commit=False)
print(instance.duration) #prints the default value every time
表单字段
duration = forms.IntegerField(widget=forms.NumberInput(attrs={'type':'range', 'step': '1', 'min': '1', 'max': '148'}), required=False)
推荐答案
两年之内!我很晚
class AdvertisePostForm(forms.ModelForm):
class Meta:
model = AdvertisePost
fields = ['duration', ...]
widgets = {
'duration': forms.TextInput(attrs={'type': 'range'})
}
希望这行得通如果您想显示它,则需要一些JavaScript,并在后端进行一些工作以将其转换为Int
Hope this works edit: You will need some javascript if you want to display it tho and some of work in the backend side to turn it into Int
这篇关于哪个Django表单字段可以向我提供< input type ="range"/> ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文