我有一个显示表单并保存相同表单的视图。首次显示视图时,我没有错误。但是,当保存(发布)到视图时,我收到以下错误:
/test6/settings/ int() 参数处的 TypeError 必须是字符串或数字,而不是 'QueryDict'
看法
@login_required
def keyword_settings(request, keyword):
keyword = Keyword.objects.get(keyword=keyword, status="Active")
# If we had a POST then get the request post values.
if request.method == 'POST':
# Populate the form with the instance.
form = KeywordFrom(request.POST, instance=keyword)
# Check we have valid data before saving trying to save.
if form.is_valid():
form.save()
form = KeywordFrom(instance=keyword, user=request.user)
context = {'form': form}
return render_to_response('sms/keyword_settings.html', context, context_instance=RequestContext(request))
形式
class KeywordFrom(forms.ModelForm):
def __init__(self, user=None, *args, **kwargs):
"""
Init method.
"""
super(KeywordFrom, self).__init__(*args, **kwargs)
if user is not None:
this_originator_name = Originator.objects.for_user(user)
self.fields['sender_name'] = forms.ModelChoiceField(queryset=this_originator_name)
class Meta:
model = Keyword
我知道问题出在 sender_name 上,但我尝试了不同的方法但没有解决。