在Django中自定义单选按钮 [英] Customizing Radio buttons in Django
本文介绍了在Django中自定义单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用Django中的自定义单选按钮创建单选按钮表单?
目前我有:
$ d
TEST_TYPE_CHOICES =('HDFS','HIVE','BOTH')
class TestForm form.Form):
#hdfs_test = forms.MultipleChoiceField()
#hive_test = forms.MultipleChoiceField()
#hdfs_hive_test = forms.MultipleChoiceField()
test_type = forms.MultipleChoiceField (required = True,widget = forms.RadioSelect(),choices = TEST_TYPE_CHOICES)
event_textarea = forms.Textarea(attrs = {'rows':'8','class':'form-control','placeholder ':'事件...','id':'event_textarea'}
我是新的到Django,在我看来, attrs =
字段允许添加自定义类。如何添加类似于我的 MultipleChoiceField
的东西?
解决方案
这样做,希望这将适合你。
从django导入表单
TEST_TYPE_CHOICES = [
('HDFS','HDFS '),
('HIVE','HIVE'),
('BOTH','HDFS和HIVE两者'),
class TestForm(forms.Form )
#hdfs_test = forms.MultipleChoiceField()
#hive_test = forms.MultipleChoiceField()
#hdfs_hive_test = forms.MultipleChoiceField()
test_type = forms.MultipleChoiceField(required =真的,widget = forms.RadioSelect(),choices = TEST_TYPE_CHOICES)
event_textarea = forms.Textarea(attrs = {'rows':'8','class':'form-control','placeholder'事件...','id':'event_textarea'}
def __init __(self,* args,** kwargs):
super(metaForm,self).__ init __(* args,** kwargs)
field.widget.attrs ['test_type'] ='你的类名称'
或者你也应该这样做:
from django import forms
TEST_TYP E_CHOICES = [
('HDFS','HDFS'),
('HIVE','HIVE'),
('BOTH','HDFS和HIVE'
class TestForm(forms.Form):
#hdfs_test = forms.MultipleChoiceField()
#hive_test = forms.MultipleChoiceField()
#hdfs_hive_test = forms.MultipleChoiceField ()
test_type = forms.MultipleChoiceField(required = True,widget = forms.RadioSelect(attrs = {'class':'你的类名在这里)},choices = TEST_TYPE_CHOICES)
event_textarea = forms。 Textarea(attrs = {'rows':'8','class':'form-control','placeholder':'Events ...','id':'event_textarea'}
< c $ c>
How do I create a radio button form with custom radio buttons in Django?
Currently I have this:
from django import forms
TEST_TYPE_CHOICES = ('HDFS', 'HIVE', 'BOTH')
class TestForm(forms.Form):
# hdfs_test = forms.MultipleChoiceField()
# hive_test = forms.MultipleChoiceField()
# hdfs_hive_test = forms.MultipleChoiceField()
test_type = forms.MultipleChoiceField(required=True, widget=forms.RadioSelect(), choices=TEST_TYPE_CHOICES)
event_textarea = forms.Textarea(attrs={'rows': '8', 'class': 'form-control', 'placeholder': 'Events...', 'id': 'event_textarea'})
I am new to Django and it seems to me the attrs=
field allows custom classes to be added. How do I add something similar to my MultipleChoiceField
?
解决方案
you should do like this, hope this will work for you.
from django import forms
TEST_TYPE_CHOICES = [
('HDFS', 'HDFS'),
('HIVE', 'HIVE'),
('BOTH', 'Both of HDFS and HIVE'),]
class TestForm(forms.Form):
# hdfs_test = forms.MultipleChoiceField()
# hive_test = forms.MultipleChoiceField()
# hdfs_hive_test = forms.MultipleChoiceField()
test_type = forms.MultipleChoiceField(required=True, widget=forms.RadioSelect(), choices=TEST_TYPE_CHOICES)
event_textarea = forms.Textarea(attrs={'rows': '8', 'class': 'form-control', 'placeholder': 'Events...', 'id': 'event_textarea'})
def __init__(self, *args, **kwargs):
super(metaForm, self).__init__(*args, **kwargs)
field.widget.attrs['test_type'] = 'your class name here'
Or you should also do like this :
from django import forms
TEST_TYPE_CHOICES = [
('HDFS', 'HDFS'),
('HIVE', 'HIVE'),
('BOTH', 'Both of HDFS and HIVE'),]
class TestForm(forms.Form):
# hdfs_test = forms.MultipleChoiceField()
# hive_test = forms.MultipleChoiceField()
# hdfs_hive_test = forms.MultipleChoiceField()
test_type = forms.MultipleChoiceField(required=True, widget=forms.RadioSelect(attrs={'class':'your class name here'}), choices=TEST_TYPE_CHOICES)
event_textarea = forms.Textarea(attrs={'rows': '8', 'class': 'form-control', 'placeholder': 'Events...', 'id': 'event_textarea'})
这篇关于在Django中自定义单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文