django - form.cleaned_data returns empty set -
i've got form:
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)
here's view:
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)
i guess problem template:
<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> ...
i guess problem don't use {{form}}
. need these styles form form. same registration template , works. registration form inherits usercreationform
, bounded user
model, orderform inherits form.form
. can be?
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)
the basic problem here within definition of form. forms don't work model fields, need form fields instead. fields built correctly.
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)
after try output {{ form }}
in template.
<form action="foo" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="submit"/> </div>
Comments
Post a Comment