In my views.py, i have a snippit of code like this:
在我的views.py中,我有一个像这样的代码:
def clean_post_data(form):
for i in form.cleaned_data:
form.cleaned_data[i] = form.cleaned_data[i].rstrip()
def add_product(request):
form = ProductForm(request.POST, request.FILES or None)
image = Image.objects.all()
action = "Add"
if request.POST:
if form.is_valid():
clean_post_data(form)
form.save()
action = "Added new product"
return render_to_response('cms/admin/action.html', {'action' : action},context_instance=RequestContext(request))
else:
action = "There was an error. Please go back and try again"
return render_to_response('cms/admin/action.html', {'action' : action}, context_instance=RequestContext(request))
return render_to_response('cms/admin/editproduct.html', {'form' : form, 'action' : action, 'image' : image}, context_instance=RequestContext(request))
But when i run that, i get the following error 'list' object has no attribute 'rstrip'
. What am i doing wrong.
但是当我运行它时,我得到以下错误'list'对象没有属性'rstrip'。我究竟做错了什么。
I originally had the for i in form.cleaned_data:
loop directly in the view (not in another function) and it worked fine, but now when i try it i get the same error as above. http://dpaste.com/92836/
我最初在form.cleaned_data中使用for i:直接在视图中循环(不在另一个函数中)并且它工作正常,但是现在当我尝试它时,我得到与上面相同的错误。 http://dpaste.com/92836/
2 个解决方案
#1
1
The clean_post_data
shouldn't be a stand-alone function.
clean_post_data不应该是一个独立的功能。
It should be a method in the form, named clean
. See Form and Field Validation.
它应该是表单中的方法,名为clean。请参阅表单和字段验证。
#2
0
Most likely you have several elements on your form with same name. When it is submitted one of the elements returned by cleaned_data is a list
很可能您的表单上有多个具有相同名称的元素。提交时,cleaning_data返回的元素之一是列表
If you want to skip (or do something special about) such cases you need to check for it:
如果你想跳过(或做一些特殊的事情)这种情况你需要检查它:
def clean_post_data(form): for i in form.cleaned_data: if('__iter__' in dir(form.cleaned_data[i])): print "skip this element: " + str(form.cleaned_data[i]) else: form.cleaned_data[i] = form.cleaned_data[i].rstrip()
#1
1
The clean_post_data
shouldn't be a stand-alone function.
clean_post_data不应该是一个独立的功能。
It should be a method in the form, named clean
. See Form and Field Validation.
它应该是表单中的方法,名为clean。请参阅表单和字段验证。
#2
0
Most likely you have several elements on your form with same name. When it is submitted one of the elements returned by cleaned_data is a list
很可能您的表单上有多个具有相同名称的元素。提交时,cleaning_data返回的元素之一是列表
If you want to skip (or do something special about) such cases you need to check for it:
如果你想跳过(或做一些特殊的事情)这种情况你需要检查它:
def clean_post_data(form): for i in form.cleaned_data: if('__iter__' in dir(form.cleaned_data[i])): print "skip this element: " + str(form.cleaned_data[i]) else: form.cleaned_data[i] = form.cleaned_data[i].rstrip()