我试图按照Django文档中的示例来为我的表单添加一个选择字段。我创建了以下选择模型:
class Ref_AccessDescription(models.Model):
FACILITY = '7'
MARKET = '6'
DIVISION = '5'
GROUP = '4'
CORPORATE = '3'
DEVELOPER = '1'
access_level_CHOICES = (
(FACILITY, 'Facility'),
(MARKET, 'Market'),
(DIVISION, 'Division'),
(GROUP, 'Group'),
(CORPORATE, 'Corporate'),
(DEVELOPER, 'Developer'),
)
access_level = models.CharField(
max_length=1,
choices=access_level_CHOICES,
default=FACILITY,
)
def is_corporate(self):
return self.access_level in (self.CORPORATE)
def is_developer(self):
return self.access_level in (self.DEVELOPER)
然后我创建以下表格来拉动所有对象:
class AccessLevelForm(forms.Form):
accesslevel = forms.ModelChoiceField(Ref_AccessDescription.objects.all())
我创建视图以使用传递我的参数的表单:
form = AccessLevelForm()
errors = form.errors or None # form not submitted or it has errors
args = {'retreivecheckbox': reportlist,'form': form,'errors': errors}
return render(request,'accounts/requestaccess.html', args)
当我尝试在模板中渲染表单时,它不会用我的选择字段填充它。
<label for="accesslevel">Access Level</label>
<select class="form-control" id="accesslevel">
<option>{{ form.accesslevel }} </option>
我还尝试过使用{{form}}并且没有任何内容被填充