I am writing a django view that just post some data and update a variable in the database. The view will not return anything as I am submitting the data using jquery-ajax.
I am getting the following error:
The view mysite.views.home didn't return an HttpResponse object
Again I do not wish to return an html page or anything of that sort. How can I achieve that?
Django views are Python functions that takes http requests and returns http response, like HTML documents. A web page that uses Django is full of views with different tasks and missions.
If you raise Http404 at any point in a view function, Django will catch it and return the standard error page for your application, along with an HTTP error code 404. In order to show customized HTML when Django returns a 404, you can create an HTML template named 404.
Function based views are the views in Django that are defined by functions. These python functions take a web request and returns a web response. Function based views are most of the time based on 'What you see is what you get approach'. This is because all the logic of the view must be included.
This guide will explore how to use views in Django as a data display and viewing tool. It will also explore the two major types of views: class-based and function-based views.
return HttpResponse("")
I usually do:
return HttpResponse("OK")
just because. In case I want to add error codes in later.
A view in Django must return a HttpResponse
, even if it's empty.
You still need to return an HttpResponse, even for an Ajax query. If you're sure it doesn't need any content, you can return an empty one:
return HttpResponse('')
but I would at least put 'ok
' to indicate to your Javascript that everything went correctly.
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