django-selectable - 从模板中保存数据时出错

时间:2012-03-01 23:57:51

标签: python django django-apps

我正在使用django-selectable,无法从模板中保存代表类别ID的“id”('autocomplete_1')。

models.py

class Category(models.Model):
cat_no = models.IntegerField(null=True, blank=True)
cat_txt = models.CharField(max_length=45)    
def __unicode__(self):
    return self.cat_txt

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

lookups.py

class CategoryLookup(ModelLookup):
model = Category
search_fields = ('cat_txt__icontains', )

forms.py

class CategoryForm(forms.Form):
autocomplete = forms.CharField(
    label='Type the name of a category (AutoCompleteWidget)',
    widget=selectable.AutoCompleteWidget(CategoryLookup),
    required=False,
)
autocompleteselect = selectable.AutoCompleteSelectField(
    lookup_class=CategoryLookup,
    label='Select a category (AutoCompleteField)',
    required=False,
)

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

views.py

def cattest(request):
if request.method == 'POST':
    form = CattestForm(request.POST)
    if form.is_valid():
        cattest = Cattest.objects.create(
            category=form.cleaned_data['autocomplete_1'],
            info=form.cleaned_data['info'],
        )
        # Always redirect after a POST
        return http.HttpResponseRedirect('/bsmain/login_customer')            
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 /

中的KeyError

'autocomplete_1'

请求方法:POST 请求网址:http://127.0.0.1:8000/bsmain/cattest/ Django版本:1.3.1 异常类型:KeyError 例外值:

'autocomplete_1'

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

请求信息

获取GET数据 POST变量值

   info             u'44# Bruce'
   csrfmiddlewaretoken     u'9ffe49bd68be04087521e71e86a5bec0'
   autocomplete_1   u'10'
   autocomplete_0   u'Anchors'

1 个答案:

答案 0 :(得分:1)

根据字段名称填充表单cleaning_data字典。您应该从form.cleaned_data['autocomplete']访问数据。

编辑:

使用ModelForm

可能会更容易
class CattestForm(forms.ModelForm):

    class Meta:
        model = Cattest
        fields = ('category', 'info', )
        widgets = {
            'category': selectable.AutoCompleteSelectWidget(CategoryLookup)
        }


def cattest(request):
    if request.method == 'POST':
        form = CattestForm(request.POST)
        if form.is_valid():
            cattest = form.save()
            # Always redirect after a POST
            return http.HttpResponseRedirect('/bsmain/login_customer')            
    else:
        initial = request.GET or None
        form = CattestForm(initial=initial)
    return render_to_response('bsmain/form.html', {'form': form}, context_instance=RequestContext(request))