(view, site)
return decorated_func
装饰WeChatAdmin中的view
    在请求上附上WeChatApp实例
    并在响应的模板上附上app,app_id等context
装饰WeChatAdmin中的view
    在请求上附上WeChatApp实例
    并在响应的模板上附上app,app_id等context
装饰WeChatAdmin中的view 在请求上附上WeChatApp实例 并在响应的模板上附上app app_id等context
def wechat_admin_view(view, site):
    """装饰WeChatAdmin中的view
    在请求上附上WeChatApp实例
    并在响应的模板上附上app,app_id等context
    """
    def correct_url(request, object_id):
        """修正请求url"""
        resolved_url = resolve(request.path_info)
        url_name = resolved_url.url_name
        url_kwargs = resolved_url.kwargs

        if object_id:
            # 对于只有object_id没有app_id的 重定向到有app_id的url
            modeladmin = view.__self__
            try:
                obj = modeladmin.model.objects.get(pk=object_id)
            except modeladmin.model.DoesNotExist:
                return response.HttpResponseBadRequest()

            url_kwargs.update(
                wechat_app_id=obj.app_id,
                object_id=object_id
            )
            return response.HttpResponseRedirect(
                reverse("admin:" + url_name, kwargs=url_kwargs), status=307
            )
        else:
            # 对于没有app_id的listview请求 优先取referrer的app_id
            referrer = request.META.get("HTTP_REFERER", "")
            path_info = urlparse(referrer).path
            try:
                app_id = resolve(path_info).kwargs["wechat_app_id"]
            except (KeyError, Resolver404):
                return response.HttpResponseNotFound()

            url_kwargs.update(wechat_app_id=app_id)
            resp = response.HttpResponseRedirect(
                reverse("admin:" + url_name, kwargs=url_kwargs)
            )
            resp.status_code = 307
            return resp

    @wraps(view)
    def decorated_func(request, *args, **kwargs):
        app_id = kwargs.pop("wechat_app_id", None)
        object_id = kwargs.get("object_id", None)
        if not app_id:
            return correct_url(request, object_id)

        extra_context = kwargs.pop("extra_context", None) or {}
        try:
            app = site.wechat_site.get_app_queryset().get(id=app_id)
        except WeChatApp.DoesNotExist:
            return response.HttpResponseNotFound()

        # 附上app
        request.app = app
        request.app_id = app_id
        # 增加模板context
        extra_context.update(
            wechat_app=app,
            wechat_app_id=app_id
        )
        kwargs["extra_context"] = extra_context
        return view(request, *args, **kwargs)

    return decorated_func
def wechat_admin_view ( view , site ) : def correct_url ( request , object_id ) : """修正请求url""" resolved_url = resolve ( request . path_info ) url_name = resolved_url . url_name url_kwargs = resolved_url . kwargs if object_id : # 对于只有object_id没有app_id的 重定向到有app_id的url modeladmin = view . __self__ try : obj = modeladmin . model . objects . get ( pk = object_id ) except modeladmin . model . DoesNotExist : return response . HttpResponseBadRequest ( ) url_kwargs . update ( wechat_app_id = obj . app_id , object_id = object_id ) return response . HttpResponseRedirect ( reverse ( "admin:" + url_name , kwargs = url_kwargs ) , status = 307 ) else : # 对于没有app_id的listview请求 优先取referrer的app_id referrer = request . META . get ( "HTTP_REFERER" , "" ) path_info = urlparse ( referrer ) . path try : app_id = resolve ( path_info ) . kwargs [ "wechat_app_id" ] except ( KeyError , Resolver404 ) : return response . HttpResponseNotFound ( ) url_kwargs . update ( wechat_app_id = app_id ) resp = response . HttpResponseRedirect ( reverse ( "admin:" + url_name , kwargs = url_kwargs ) ) resp . status_code = 307 return resp @ wraps ( view ) def decorated_func ( request , * args , * * kwargs ) : app_id = kwargs . pop ( "wechat_app_id" , None ) object_id = kwargs . get ( "object_id" , None ) if not app_id : return correct_url ( request , object_id ) extra_context = kwargs . pop ( "extra_context" , None ) or { } try : app = site . wechat_site . get_app_queryset ( ) . get ( id = app_id ) except WeChatApp . DoesNotExist : return response . HttpResponseNotFound ( ) # 附上app request . app = app request . app_id = app_id # 增加模板context extra_context . update ( wechat_app = app , wechat_app_id = app_id ) kwargs [ "extra_context" ] = extra_context return view ( request , * args , * * kwargs ) return decorated_func