I have this view:
def send_results(request):
print request
if request.is_ajax():
address = request.POST.get('url')
process_data(address)
context = get_all_from_database()
return HttpResponse(json.dumps(context), content_type='application/json')
and I need to test it:
def test_send_results(self):
factory = RequestFactory()
request = factory.get('/views/send_results')
response = send_results(request)
self.assertEqual(response.status_code, 200)
But it always stop with error that in my view 'address' value is referensed before asignment. How to pass it some value ?
If the request.is_ajax()
is False
then address
will not be assigned before process_data(address)
is called. If you want to test an AJAX request then you should pass the HTTP_X_REQUESTED_WITH
header:
request = factory.get('/views/send_results', HTTP_X_REQUESTED_WITH='XMLHttpRequest')
However you'll still need to fix your view to handle the case when the request is not an AJAX request.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With