试图寻找解决方案,但我遇到了另一个问题,找不到与之相关的任何东西 我已经使用来自models.py
的字段创建了一个django表单forms.py
from .models import ProValue
from django import forms
class SchoolInput(forms.ModelForm):
school_name = forms.CharField(
max_length=255, required=True, widget=forms.TextInput(
attrs={
'class': 'input'
}
))
level_of_grades = forms.IntegerField(min_value=1, required=True)
num_of_days = forms.IntegerField(min_value=1, max_value=7, required=True)
division_check = forms.ChoiceField(
label='Do you have multiple divisions for your grades?', widget=forms.RadioSelect)
class Meta:
model = ProValue
answer = (
('no', 'no'),
('yes', 'yes')
)
fields = [
'school_name',
'level_of_grades',
'num_of_days',
'division_check',
]
这样,我在页面上看不到单选按钮 我已经尝试过了
division_check = forms.ChoiceField(
label='Do you have multiple divisions for your grades?', widget=forms.RadioSelect())
和
division_check = forms.ChoiceField(
label='Do you have multiple divisions for your grades?', widget=forms.RadioSelect(choice=answer))
这告诉我答案未定义
这是我的模特。py
from django.db import models
from django import forms
class ProValue(models.Model):
school_name = models.CharField(max_length=50)
level_of_grades = models.IntegerField()
num_of_days = models.IntegerField()
answer = (
('Yes', 'Yes'),
('No', 'No'),
)
division_check = models.CharField(
max_length=5, choices=answer)
此外,我是否需要再次在Meta类中定义答案?起初我没有这样做,我试图解决上述问题,但仍然无法解决。
答案 0 :(得分:0)
您在answer
类中定义了Meta
,但是在定义窗口小部件时不在范围内。
您可以引用模型中的选项,例如:
from .models import ProValue
from django import forms
class SchoolInput(forms.ModelForm):
school_name = forms.CharField(
max_length=255, required=True, widget=forms.TextInput(
attrs={
'class': 'input'
}
))
level_of_grades = forms.IntegerField(min_value=1, required=True)
num_of_days = forms.IntegerField(min_value=1, max_value=7, required=True)
division_check = forms.ChoiceField(
label='Do you have multiple divisions for your grades?',
choices=ProValue.answer,
widget=forms.RadioSelect
)
class Meta:
model = ProValue
fields = [
'school_name',
'level_of_grades',
'num_of_days',
'division_check',
]
答案 1 :(得分:0)
您的模型很好,您可以从表单中删除自定义字段division_check
,只需更改表单Meta
中的小部件和标签即可
class Meta:
model = ProValue
fields = [
'school_name',
'level_of_grades',
'num_of_days',
'division_check',
]
widgets = {
'division_check': forms.RadioSelect,
}
labels = {
'division_check': 'Do you have multiple divisions for your grades?',
}