diff --git a/admin/middleware.py b/admin/middleware.py new file mode 100644 index 00000000..515a8893 --- /dev/null +++ b/admin/middleware.py @@ -0,0 +1,16 @@ +# coding=utf-8 +import json + +from django.http import HttpResponse, HttpResponseRedirect + + +class AdminRequiredMiddleware(object): + def process_request(self, request): + path = request.path_info + if path.startswith("/admin/") or path.startswith("/api/admin/"): + if not request.user.is_authenticated(): + if request.is_ajax(): + return HttpResponse(json.dumps({"code": 1, "data": u"请先登录"}), + content_type="application/json") + else: + return HttpResponseRedirect("/login/") \ No newline at end of file