'IntegerField'对象没有属性'value_from_datadict'DJANGO

时间:2015-02-13 11:12:30

标签: python django session shop

欢迎,我试图根据会议建立一个商店。不幸的是,我发现了一个我无法找到答案的错误。堆栈上的人说这个表单没有被正确引用。我尝试以各种不同的方式进行参考,但非帮助。当应用程序尝试通过它验证表单时会抛出一个错误:“'IntegerField'对象没有属性'value_from_datadict'”

这是我的views.py

def add_to_cart(request, category_id):

if request.method == 'POST':
    #add_product_form = AddProductForm(data = request.POST)
    add_product_form = AddProductForm(data = request.POST, request_product_id=request.POST['product_id'])
    print "%$$$$$$$$$$$$$$$$$ ADD PROFUCT FORM"
    print add_product_form

    if add_product_form.is_valid():
        if not request.session.get('koszyk').get(add_product_form.product_id, None):
            request.session['koszyk'][add_product_form.product_id] = 0
        request.session['koszyk'][add_product_form.product_id] += add_product_form.quantity
        messages.success(request, 'Category added')
        return redirect('tango/products.html')
    else:
        print add_product_form.errors
        messages.error(request, 'Smth went wrong, check log')
else:
    add_product_form = AddProductForm()

print request.session['koszyk'][add_product_form.product_id]
koszyk = request.session['koszyk']
products_list = ShopProduct.objects.all()

print request.session['koszyk']
return render(request, 'tango/cart.html', {'koszyk': koszyk, 'products_list': products_list})

forms.py:

class AddProductForm(forms.ModelForm):
quantity = forms.IntegerField(widget=forms.IntegerField)

def __init__(self, *args, **kwargs):
    # print self.request_product
    self.request_product_id = kwargs.pop('request_product_id', None)
    super(AddProductForm, self).__init__(*args, **kwargs)

    print kwargs

   # self.fields['quantity'].widget.attrs['id'] = "id_quantity_{0}".format(self.request_product_id)


class Meta:
    model = ShopProduct
    fields = ('quantity',)

models.py:

class ShopProduct(models.Model):
    name = models.CharField(max_length = 100, blank = True, null = True)
    category = models.ForeignKey(ShopCategory, blank = True, null = True)
    price = models.FloatField(default = 0)
    stock = models.IntegerField(default = 0)

我的模板中的最后一个表单:

            <form id="custom_user_form" method="post" action="add_product/">
            <td>
                {% csrf_token %}

                {{ add_product_form.as_p }}

                <!-- Provide a button to click to submit the form. -->
                <input id="product_id" type="number" name="product_id" value={{p.id}} hidden="True" />
                <input type="number" id="quantity" name="quantity" value="0" />
            </td>
            <td>
                <button type="submit"  class="btn btn-primary">Add</button>
            </td>
        </form>

1 个答案:

答案 0 :(得分:1)

widget表单字段定义中删除quantity参数:

class AddProductForm(forms.ModelForm):
    quantity = forms.IntegerField()

我还注意到您错误地尝试从表单中获取quantity值。为此,您应该使用cleaned_data属性:

request.session['koszyk'][add_product_form.product_id] += \
                                  add_product_form.cleaned_data['quantity']