• [python]pyramid 学习3 (route)


    模式1: main中add_view

    projectname/__init__.py main方法中加入

    from views import myview
    config.add_route('myroute', '/prefix/{one}/{two}')
    config.add_view(myview, route_name='myroute')

    projectname/views.py中加入方法

    from pyramid.response import Response
    def myview(request):
    return Response(request.matchdict['one']+"#"+request.matchdict['two'])
    浏览器访问
    127.0.0.1:6543/prefix/1/2 查看效果

     

    模式2 main中使用scan

    projectname/__init__.py main方法中加入路由规则

    config.add_route('scan','/scan/{one}')
    config.scan("projectname")

    projectname/views.py中加入方法

    from pyramid.response import Response
    from pyramid.view import view_config

    @view_config(route_name="scan")
    def scan(request):
    return return Response(request.matchdict['one'])
    浏览器访问
    127.0.0.1:6543/scan/test 查看效果


    路由语法规则

    *

    /foo/*fizzle
    /foo/La%20Pe%C3%B1a/a/b/c
    {'fizzle':(u'La Pe\xf1a', u'a', u'b', u'c')}

    .

    foo/{baz}/{bar}{fizzle:.*}
    foo/1/2/ -> {'baz':u'1', 'bar':u'2', 'fizzle':()}
    foo/abc/def/a/b/c -> {'baz':u'abc', 'bar':u'def', 'fizzle': u'a/b/c')}

    生成url

    config.add_route('scan','/scan/{one}')
    url = request.route_url('scan', one='1000')

    404

    config.add_view('pyramid.view.append_slash_notfound_view',
    context='pyramid.httpexceptions.HTTPNotFound')

    自定义404

    from pyramid.httpexceptions import HTTPNotFound
    from pyramid.view import AppendSlashNotFoundViewFactory

    def notfound_view(context, request):
    return HTTPNotFound('It aint there, stop trying!')

    custom_append_slash = AppendSlashNotFoundViewFactory(notfound_view)
    config.add_view(custom_append_slash, context=HTTPNotFound)

    route debug

    PYRAMID_DEBUG_ROUTEMATCH=true \ paster serve development.ini

    route include

    def add_views(config):
    config.add_view('myapp.views.view1', name='view1')
    config.add_view('myapp.views.view2', name='view2')

    config.include(add_views)

    include prefix

    def users_include(config):
    config.add_route('show_users', '/show')

    config.include(users_include, route_prefix='/users')
    /users/show


    Custom Route

    def any_of(segment_name, *allowed):
    def predicate(info, request):
    if info['match'][segment_name] in allowed:
    return True
    return predicate

    num_one_two_or_three = any_of('num', 'one', 'two', 'three')

    config.add_route('route_to_num', '/{num}',
    custom_predicates=(num_one_two_or_three,))
    /one
    /two
    /three
    def integers(*segment_names):
    def predicate(info, request):
    match = info['match']
    for segment_name in segment_names:
    try:
    match[segment_name] = int(match[segment_name])
    except (TypeError, ValueError):
    pass
    return True
    return predicate

    ymd_to_int = integers('year', 'month', 'day')

    config.add_route('ymd', '/{year}/{month}/{day}',
    custom_predicates=(ymd_to_int,))
  • 相关阅读:
    敏捷开发 第18章 薪水支付案例研究:第一次迭代开始
    敏捷软件开发 13~16
    pyqt5与QML开发小结
    【Forge】Minecraft 1.7.10 Mod开发研究
    【Forge】Minecraft 1.7.10 Mod开发研究
    【Forge】Minecraft 1.7.10 Mod开发研究
    我写了个屎
    我要更新了!
    背景设定(暂定)
    砍手就砍手
  • 原文地址:https://www.cnblogs.com/bluefrog/p/2218373.html
Copyright © 2020-2023  润新知