我有 django 3.2,最新的脆皮形式。
使用 bs 模板:CRISPY_TEMPLATE_PACK = "bootstrap5"
从这里
我正在使用 ModelView 和 Model 表单。
class HspRegistrationForm(ModelForm):
def __init__(self, *args, **kwargs):
super(HspRegistrationForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(self)
self.helper.form_id = 'id-hspr'
self.helper.layout.append(Submit('save', 'save'))
class Meta:
model = HomeServiceProvider
fields = [...]
class HspRegistrationView(CreateView, UserPassesTestMixin):
form_class = HspRegistrationForm
# sepcify name of template
template_name = "mainss/hsp_registration.html"
success_url = "/"
def test_func(self):
return self.request.user.is_active
def form_valid(self, form):
print(form.cleaned_data)
return super().form_valid(form)
表格正在正确生成,示例如下:
<form id="id-hspr" method="post" > <input type="hidden" name="csrfmiddlewaretoken" value="..."> <div id="div_id_bus_name" class="mb-3"> <label for="id_bus_name" class="form-label requiredField">Business Name<span class="asteriskField">*</span> </label> <input type="text" name="bus_name" maxlength="200" class="textinput textInput form-control" required id="id_bus_name"> <small id="hint_id_bus_name" class="form-text text-muted">Name of the HSP business</small> </div>
....
</form>
当我尝试使用无效的内容(如必填字段)提交时,它会自动将我带到该字段以指示它需要填写。
但是,在我阅读的教程和文档中,没有任何明显的变化,例如错误消息或突出显示。