form.cleaned_data返回空集

时间:2015-04-25 19:34:22

标签: django forms django-models django-forms

我有一张表格:

class OrderForm(forms.Form):
    delivery_time = models.CharField(max_length=100)
    address_city = models.CharField(max_length=40)
    address_street = models.CharField(max_length=40)
    address_building = models.CharField(max_length=40)

以下是我的观点:

def submit(request):
    args = {}
    args['form'] = OrderForm()
    if request.POST:
        order_form = OrderForm(request.POST)
        if order_form.is_valid():
            user = request.user
            address_city = order_form.cleaned_data.get('address_city')
            address_street = order_form.cleaned_data.get('address_street')
            address_building = order_form.cleaned_data.get('address_building')
            delivery_time = order_form.cleaned_data.get('delivery_time')
            new_order = Order(address_city=address_city,
                              address_street=address_street,
                              address_building=address_building,
                              delivery_date=delivery_time, client=user)
            new_order.save()
            Basket.objects.filter(client=user).delete()
            return redirect('/order/')
        else:
            args['form'] = order_form
    else:
        return render(request, 'order_page.html', args)

我想问题出在我的模板上:

<form class="form-horizontal" action="{% url 'order_submit' %}" method="post">
        {% csrf_token %}   
        <div class="control-group">
            <label class="control-label" for="address">City<sup>*</sup></label>
            <div class="controls">
                <input type="text" id="address" placeholder="City name" name="address_city">
            </div>
        </div>

        ...

我想问题是我不只使用{{form}}。但是我需要从形式中确切地说这些样式。我对我的注册模板做了同样的工作。注册表单继承UserCreationForm并与User模型绑定,但我的OrderForm继承form.Form。它能是什么?

Order model:
class Order(models.Model):
    state_choices = ('ACTIVE', 'COMPLETED', 'FROZEN')
    order_date = models.DateTimeField(auto_now_add=True)
    delivery_time = models.CharField(max_length=100)
    address_city = models.CharField(max_length=40)
    address_street = models.CharField(max_length=40)
    address_building = models.CharField(max_length=40)
    state = models.CharField(max_length=200, default='ACTIVE')
    client = models.ForeignKey(CustomUser)

2 个答案:

答案 0 :(得分:3)

这里的基本问题在于表单的定义。表单不适用于模型字段,而是需要表单字段。只有这样才能正确构建字段。

EntityState

之后尝试在模板中输出from django import forms class OrderForm(forms.Form): delivery_time = forms.CharField(max_length=100) address_city = forms.CharField(max_length=40) address_street = forms.CharField(max_length=40) address_building = forms.CharField(max_length=40)

{{ form }}

答案 1 :(得分:2)

您不应创建form.Form类来明确处理每个字段。您可以从forms.ModelForm继承子类,让模型表单为您处理所有内容。

sthzg已经指出了问题,但我仍然坚持你尽可能使用普通形式的模型表格。

以下是表单类和视图的重写版本:

<强> forms.py

class OrderForm(forms.ModelForm):
    class Meta:
        model = Order
        exclude = ['order_date', 'client', 'state']

<强> views.py

def submit(request):
    if request.method == 'POST':
        form = OrderForm(initial={'client': request.user}, data=request.POST)
        if form.is_valid():
            order = form.save(commit=False)
            order.client = request.user
            order.save()
            Basket.objects.filter(client=request.user).delete()
            return redirect('/order/')
        else:
            return render(request, 'order_page.html', {'form': form})
    else:
        return render(request, 'order_page.html', {'form': OrderForm()})