问题描述
我有一个视图显示一个表单并保存相同的表单。当我第一次显示视图时,我没有错误。但是,当保存(POST))到视图时,我得到以下错误: / test6 / settings /
中的TypeError int()参数必须作为字符串或数字,而不是QueryDict
查看
关键字= Keyword.objects.get(关键字=关键字,状态=活动)
$ b def关键字_settings(请求,关键字)
$ b#如果我们有一个POST,然后获取请求的帖子值。
如果request.method =='POST':
#填写实例的表单。
form = KeywordFrom(request.POST,instance = keyword)
#检查我们有保存之前有效的数据保存。
如果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方法
super如果用户不是没有,
this_originator_name = Originator.objects.for_user(user)
self.fields ['sender_name'] = forms.ModelChoiceField(queryset = this_originator_name)
class Meta:
model =关键字
我知道问题是与sender_name,但我已经尝试了不同的事情,没有解决。
查看您如何覆盖您的表单的 __ init __
方法:
def __init __(self,user = None,* args,** kwargs):
$ p $所以第一个显式参数(隐含的
self
之后)是user
。
现在看看你在POST块中如何实例化表单:
form = KeywordFrom(request.POST,instance = keyword)
所以你通过的第一个参数是POST。但是,该表单期待在该位置的
用户
,并将任何您在那里传入的变量。
您在POST实例化时需要传递用户,这将解决问题。但这表明为什么这是一个糟糕的主意,混乱一个类的默认签名。您应该真正保留签名,并从
kwargs
中的用户
:
$ b $
$($ user $,$)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $自我).__ init __(* args,** kwargs)
...等...
I have a view which displays a form and saves the same form. When first showing the view I have no errors. However, when saving (POSTing) to the view I get the following error:
TypeError at /test6/settings/int() argument must be a string or a number, not 'QueryDict'
View
@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))
Form
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
I know the issue is with the sender_name but I have tried different things with no resolve.
Look at how you've overridden the __init__
method of your form:
def __init__(self, user=None, *args, **kwargs):
So the first explicit parameter (after the implicit self
) is user
.
Now look at how you're instantiating the form in the POST block:
form = KeywordFrom(request.POST, instance=keyword)
So the first parameter you're passing is the POST. But the form is expecting user
in that position, and will put whatever you pass there into that variable.
You need to pass the user in when you instantiate on POST, which will fix the problem. But this indicates why it's a bad idea to mess around with the default signature of a class. You should really preserve the signature and take the user
from kwargs
:
def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super(KeywordFrom, self).__init__(*args, **kwargs)
...etc...
这篇关于Django int()参数必须是字符串或数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!