1

有人可以告诉我我做错了什么吗?

模型.py

class Cattest(models.Model):
category = models.ForeignKey(Category)    
info = models.CharField(max_length=35, blank=True)    

表格.py

class CattestForm(forms.Form):
autocomplete = forms.CharField(
    label='Type the name of a category (AutoCompleteWidget)',
    widget=selectable.AutoCompleteWidget(CategoryLookup),
    required=False,
)
info = forms.CharField(max_length=35, label="Information")   

视图.py

def cattest(request):
if request.method == 'POST':
    form = CattestForm(request.POST)
    if form.is_valid():
        cattest.save()        
else:
    if request.GET:
        form = CattestForm(initial=request.GET)
    else:
        form = CattestForm()
return render_to_response('bsmain/form.html', {'form': form}, context_instance=RequestContext(request))    

追溯:

/bsmain/cattest/ 处的 AttributeError

'function' 对象没有属性 'save'

请求方法:POST 请求 URL: http: //127.0.0.1 :8000/bsmain/cattest/ Django 版本:1.3.1 异常类型:AttributeError 异常值:

'function' 对象没有属性 'save'

异常位置:cattest 中的 /home/bill/workspace/boatsite/../boatsite/bsmain/views.py,第 50 行 Python 可执行文件:/usr/bin/python Python 版本:2.6.5

4

3 回答 3

3

You have a type-o in your view.

you are calling save() on the function you are in cattest.save() I think this should be on the form?? You might want to look at ModelForm it provides a form that maps directly to your model, which it looks like you are doing.

https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#django.forms.ModelForm

The form you are using is just a normal form and doesn't have a save method. After subclassing ModelForm you can then call save on a form instance.

if your_form.is_valid():
  your_form.save()
于 2012-02-28T02:47:53.330 回答
1

您正在函数本身上调用 save 方法。这就是错误背后的原因。如果您正在寻找要保存的表格,那么:

if form.is_valid():
    form.save()
于 2018-04-20T21:43:21.553 回答
-1

class CattestForm(forms.Form):应该class CattestForm(forms.ModelForm):

于 2019-08-09T21:15:17.017 回答