Last active
June 9, 2016 15:36
-
-
Save shinsaka/097800f6b4525538fe846a3fb1e0614b to your computer and use it in GitHub Desktop.
Decorator for determing whether the request is Ajax, in Django-views.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ajax_required(ret_unexcepted): | |
""" | |
Decorator for determing whether the request is Ajax, in Django-views. | |
e.g.) in views.py | |
from django.http import HttpResponseBadRequest | |
from utils.decorators import ajax_required | |
@ajax_requirxed(HttpResponseBadRequest()) | |
def index(request): | |
pass | |
""" | |
def _ajax_required(func): | |
def wrapper(request, *args, **kwargs): | |
if not request.is_ajax(): | |
return ret_unexcepted | |
return func(request, *args, **kwargs) | |
return wrapper | |
return _ajax_required |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponseBadRequest | |
from ajax_required_decorator import ajax_required | |
@ajax_required(HttpResponseBadRequest()) | |
def index(request): | |
# any code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment