问题
I've got a html form with this input:
<input id="duration_slider" type="range" min="1" max="168" value="48" />
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
.
What would my models.py and forms.py looks like for this field?
Currently it's just:
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'
]
EDIT
views
if options_form.is_valid():
instance = options_form.save(commit=False)
print(instance.duration) #prints the default value every time
form field
duration = forms.IntegerField(widget=forms.NumberInput(attrs={'type':'range', 'step': '1', 'min': '1', 'max': '148'}), required=False)
回答1:
Oof two years! I'm very late
class AdvertisePostForm(forms.ModelForm):
class Meta:
model = AdvertisePost
fields = ['duration', ...]
widgets = {
'duration': forms.TextInput(attrs={'type': 'range'})
}
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
来源:https://stackoverflow.com/questions/49772684/which-django-form-field-can-provide-me-a-html-output-of-input-type-range