jeudi 5 mars 2015

POST checkbox value in Django Unit Test

I'm trying to test a POST request in Django Unit Test and this POST request will be redirected to other url, but response isn't redirected and when I print the response I found there is an error message that checkbox is required, and it's the reason my request don't redirected.


Error:


AssertionError: Response didn't redirect as expected: Response code was 200 (expected 302)


HTML



<p>
<label for="id_is_urgent">Is urgent:</label>
<input checked="checked" id="id_is_urgent" name="is_urgent" type="checkbox" />
</p>
<ul class="errorlist">
<li>This field is required.</li>
</ul>


test_views.py



def test_post_order_details_view(self):
expected_url_for_order_list = reverse('order_update', args=(self.order.id,))
response = self.client.post(reverse('order_update', args=(self.order.id,)),
{'customer': str(self.customer.id),
'service': str(self.service.id),
'order_type': str(self.order_type.id),
'assigned_to': str(self.user.id),
'status': str(self.order_status.id),
'order_status_action': str(self.order_status_action.id),
'is_urgent': True}, follow=True
self.assertEqual(response.status_code, 200)


view.py



def order_update(request, pk, template_name='orders_app/order_details.html'):
order = get_object_or_404(CustomerOrder, pk=pk)
order_lines = OrderLine.objects.filter(order_id=pk)
customer = order.customer
service = order.service
contacts = order.customer.customer_contacts_info.all()

form = OrderForm(request.POST or None, instance=order)

if request.method == 'POST':
if form.is_valid():
order = form.save()
messages.info(request, 'Customer Order %s save successfully' % order.id)
return redirect(reverse('order_update', args=(order.id,)))
return render(request, template_name, {'form': form,
'order': order,
'lines': order_lines,
'customer': customer,
'service': service,
'contacts': contacts})

Aucun commentaire:

Enregistrer un commentaire