Reputation: 9869
I am a little bit confusing with docs. I can't understand real use case when I should check request to is_ajax()
?
Upvotes: 1
Views: 1991
Reputation: 77912
It does not make much sense in a view IMHO - if your project is well designed, you know which views are expected to be invoked via ajax (and exclusively via ajax - havin the same view called either directly or via ajax is sure design smell) and which are not -, but it can be very useful in middlewares when you want to take some action depending on the request's (and eventually response) headers.
Upvotes: 1
Reputation: 21834
Best use case of is_ajax()
is if you want to send different data to the client depending on the type of the request.
For example, if the request is not ajax, you may want to render a full page. But if the request is ajax, you may want to only send a json response.
Example:
def my_view(request):
if request.is_ajax():
return <json data>
else:
return render(...)
Upvotes: 3