在ModelForm上使用Django Crispy Forms时,我总是收到一个错误:Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited;
根据this网站,我必须这样做:
class HotelImageForm(ModelForm):
helper = FormHelper()
helper.form_tag = False
helper.layout = Layout(
"image",
"alt_name"
)
class Meta:
model = HotelImage
当我这样做的时候,我得到了我提到的错误。在
然而,当我把字段放在那里时,酥脆的表格似乎没有多大作用。在
^{pr2}$我还添加了def __init__
,但这也没用
class HotelImageForm(ModelForm):
def __init__(self, *args, **kwargs):
super(HotelImageForm, self).__init__(*args, **kwargs)
helper = FormHelper()
helper.form_tag = False
helper.layout = Layout(
"image",
"alt_name"
)
class Meta:
model = HotelImage
以下是视图中的代码:
def hotel_registration(request):
if request.method == 'POST':
hotel_form = HotelForm(request.POST, instance=Hotel())
hotel_image_form = HotelImageForm(request.POST, instance=HotelImage())
if hotel_form.is_valid():
new_hotel = hotel_form.save()
hotel_image = hotel_image_form.save()
hotel_image.hotel = new_hotel
employee.save()
employee.hotel.add(new_hotel)
return HttpResponseRedirect(reverse(hotel_registered))
else:
hotel_form = HotelForm(instance=Hotel())
hotel_image_form = HotelImageForm(instance=HotelImage())
context = {
'hotel_form': hotel_form,
'hotel_image_form': hotel_image_form,
}
context.update(csrf(request))
return render_to_response('hotel/hotel-registration-form.html', context)
以及模板中的代码:
<div class="col-md-5">
{{ hotel_form|crispy }}
{% crispy hotel_image_form hotel_image_form.helper %}
<input type="submit" class="btn btn-primary btn-lg margin-bottom-20" value="Registreren">
</div>
hotel_form
目前在表单代码中不使用Crispy表单。在
我希望有人能告诉我我这里缺少什么。在
谢谢。在
更新:
你试过
fields = '__all__'
或者
尝试将助手的呈现未提及的字段属性设置为true检查文档here。它看起来像这样:
helper.render_unmentioned_fields=True
相关问题 更多 >
编程相关推荐