Django is_ajax历史记录返回

I wrote a Django view that responses ether a text/html or a application/json depending on request.is_ajax(). So far so good, but when I use my browsers history buttons, I end up getting a JSON response rather than the HTML.

I can't figure out the problem. It's true an jQuery ajax request is getting the same url after the page was loaded, but that shouldn't end up in the history, or should it?

Thanks, Joe

If you send different content depending on request.is_ajax(), you need to send Vary: X-Requested-With to the browser. That way, the browser will be able to distinguish the two kinds of response based on the value of the X-Requested-With header on the request. You can do that via:

from django.views.decorators.vary import vary_on_headers

@vary_on_headers('X-Requested-With')
def yourview(request, ...):
    pass