Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a django view that does not return an object

Tags:

python

django

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?

like image 450
Sam Crawson Avatar asked Apr 12 '11 19:04

Sam Crawson


People also ask

What can a Django view return?

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.

How do I increase my 404 in Django?

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.

What is function based view in Django?

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.

Which are the two basic categories of views in Django?

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.


3 Answers

return HttpResponse("")

I usually do:

return HttpResponse("OK")

just because. In case I want to add error codes in later.

like image 199
Adam Vandenberg Avatar answered Oct 18 '22 23:10

Adam Vandenberg


A view in Django must return a HttpResponse, even if it's empty.

like image 44
Ignacio Vazquez-Abrams Avatar answered Oct 18 '22 23:10

Ignacio Vazquez-Abrams


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.

like image 2
Daniel Roseman Avatar answered Oct 19 '22 00:10

Daniel Roseman