看Flask教程关于Pluggable Views的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
levon
V2EX    Flask

看Flask教程关于Pluggable Views的问题

  •  
  •   levon 2012-07-21 10:01:03 +08:00 4805 次点击
    这是一个创建于 4831 天前的主题,其中的信息可能已经有所发展或是发生改变。
    app.add_url_rue('/users/', ShowUsers.as_view('show_users'))

    后面的as_view函数的参数是具体怎么理解,看不懂教程上说的“The string you pass to that function is the name of the endpoint that view will then have.”
    1 条回复    1970-01-01 08:00:00 +08:00
    dreampuf
        1
    dreampuf  
       2012-07-21 11:53:19 +08:00   1
    碰巧在看Flask的实现。实质是将一个Class Instance 转换为一个(或者多个,随Class 的dispatch_request方法的实现而不同)视图。
    直接上代码或许比文档更直接。

    @classmethod
    def as_view(cls, name, *class_args, **class_kwargs):
    """Converts the class into an actual view function that can be
    used with the routing system. What it does internally is generating
    a function on the fly that will instanciate the :class:`View`
    on each request and call the :meth:`dispatch_request` method on it.

    The arguments passed to :meth:`as_view` are forwarded to the
    constructor of the class.
    """
    def view(*args, **kwargs):
    self = view.view_class(*class_args, **class_kwargs)
    return self.dispatch_request(*args, **kwargs)

    if cls.decorators:
    view.__name__ = name
    view.__module__ = cls.__module__
    for decorator in cls.decorators:
    view = decorator(view)

    # we attach the view class to the view function for two reasons:
    # first of all it allows us to easily figure out what class based
    # view this thing came from, secondly it's also used for instanciating
    # the view class so you can actually replace it with something else
    # for testing purposes and debugging.
    view.view_class = cls
    view.__name__ = name
    view.__doc__ = cls.__doc__
    view.__module__ = cls.__module__
    view.methods = cls.methods
    return view


    使用举例:

    class MethodView(View):
    """Like a regular class based view but that dispatches requests to
    particular methods. For instance if you implement a method called
    :meth:`get` it means you will response to ``'GET'`` requests and
    the :meth:`dispatch_request` implementation will automatically
    forward your request to that. Also :attr:`options` is set for you
    automatically::

    class CounterAPI(MethodView):

    def get(self):
    return session.get('counter', 0)

    def post(self):
    session['counter'] = session.get('counter', 0) + 1
    return 'OK'

    app.add_url_rule('/counter', view_func=CounterAPI.as_view('counter'))
    """
    __metaclass__ = MethodViewType

    def dispatch_request(self, *args, **kwargs):
    meth = getattr(self, request.method.lower(), None)
    # if the request method is HEAD and we don't have a handler for it
    # retry with GET
    if meth is None and request.method == 'HEAD':
    meth = getattr(self, 'get', None)
    assert meth is not None, 'Unimplemented method %r' % request.method
    return meth(*args, **kwargs)
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2672 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 21ms UTC 02:40 PVG 10:40 LAX 19:40 JFK 22:40
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86