当我在forms.ModelMultipleChoiceField
的{{1}}中添加ProfileUpdateForm
时,提交按钮将停止运行,并且forms.py
将不会执行。
控制台说
request.POST
我已将An invalid form control with name='interest' is not focusable.
放在'name':'interest'
中,但什么也没有。
forms.py
forms.SelectMultiple(attrs={
views.py
class ProfileUpdateForm(forms.ModelForm):
interests = Interest.objects.all()
interest = forms.ModelMultipleChoiceField(
queryset=interests,
widget = forms.SelectMultiple(attrs={
'name': "interest",
'data-placeholder': "Choose your interests",
'class': 'chosen-select',
'multiple tabindex': '4',
}))
groups = Group.objects.all()
group = forms.ModelMultipleChoiceField(
queryset=groups,
widget = forms.SelectMultiple(attrs={
'name': "group",
'data-placeholder': "Choose any groups",
'class': 'chosen-select',
'multiple tabindex': '4',
}))
class Meta:
model = Profile
fields = ['interest', 'group', 'age']
profile.html
def profile_view(request):
if request.method == 'POST':
p_form = ProfileUpdateForm(request.POST,
request.FILES,
instance=request.user.profile)
if p_form.is_valid():
p_form.save()
messages.success(request, f'Your account has been updated!')
return redirect('profile')
else:
p_form = ProfileUpdateForm(instance=request.user.profile)
context = {
'p_form': p_form,
}
return render(request, 'users/profile.html', context)