1 2 3 4 5 | class AjaxCheckMiddleware(object):
def process_request(self, request):
is_ajax = request.META.get('HTTP_X_REQUESTED_WITH', None) == 'XMLHttpRequest'
setattr(request, 'is_ajax', is_ajax)
return None
|
More like this
- Workaround Firefox bug 553888 by peroksid 2 years, 3 months ago
- JSON decorator for views handling ajax requests by anilshanbhag 5 months, 4 weeks ago
- Format transition middleware by limodou 6 years, 3 months ago
- Ajax progress bar by ebartels 5 years, 2 months ago
- Simple Exception Response for AJAX debugging by newmaniese 5 years, 3 months ago
Comments
Nice. I'm just now working on something that I wanted to use the idea of "progressive enhancement" and went about this a slightly different way. But this is nice.
Is the "HTTP_X_REQUESTED_WITH" header common in all browsers?
#
Sorry, I suppose the header would have to be common in the Javascript library you use since it would be sending headers. Thanks.
#
I figured out that you can just use request,is_ajax in the template without using any middleware:
{% if request.is_ajax == True %} ajax specific html. {% endif %}
#