web.py 框架开发网站,当路由过多,怎么分文件编写? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
tomoya92
V2EX    Python

web.py 框架开发网站,当路由过多,怎么分文件编写?

  •  
  •   tomoya92 2016-01-20 22:06:09 +08:00 3954 次点击
    这是一个创建于 3575 天前的主题,其中的信息可能已经有所发展或是发生改变。

    照着官方给的教程,体验了一下 web.py 框架的简单强大,但当网站的路由稍微多点的话,在一个文件里编写 urls 的实现,就不太好了,我首先想到的是 import 其他的文件进来,这样在主文件里将 urls 配置好,在其他分模块的文件里编写实现就好,但我想将 urls 也一块带到分模块文件里,该怎么操作?

    例如:一个论坛,有帖子,回复,用户等模块,每个模块都对应一个 xx.py 文件,在每个文件里定义 urls ,然后实现。

    6 条回复    2016-01-26 16:13:48 +08:00
    laotaitai
        1
    laotaitai  
       2016-01-20 22:13:51 +08:00
    很简单嘛, 在主文件里对各个分模块文件里的 urls 进行合并即可. 框架不是限制你思维的, 是减少你任务量的.
    est
        2
    est  
       2016-01-20 22:19:53 +08:00
    blueprint
    lichun
        3
    lichun  
       2016-01-21 11:14:16 +08:00
    换 Django
    gemini
        4
    gemini  
       2016-01-21 16:25:14 +08:00   1
    这个问题之前也困扰过,挺有趣。我们用 uswgi 启动 python 应用的,启动时”自动“对应的业务模块。

    对每个模块约定文件结构,比如直接是 xxx.py 或者子目录且入口类文件名与子目录名称一致(当然也可以约定其他的)

    <pre><code>
    ## import apps/目录下所有应用
    def auto_add_route():
    excludes = ".svn common"
    routes = []

    for f in os.listdir(os.getcwd()): ## the same dir with gops.py
    if f in excludes:
    continue

    importstr = ""
    fn = f
    ispkg = True
    if os.path.isdir(f):
    importstr = "from %s import %s" %(f, f)
    else:
    ispkg = False
    if f.endswith('.py'):
    fn, fe = f.split('.')
    importstr = "import %s" %fn

    if len(importstr) == 0:
    continue

    try:
    exec importstr
    except ImportError:
    print "[ERROR] import failed, [%s]" %importstr
    pass

    ## add route
    r = "/myproject/apps/" + fn
    classname = fn
    mod = ""
    try:
    mod = __import__(classname, None, None, [''])
    except ImportError:
    print "[ERROR] import failed, [%s]" % classname
    pass

    if ispkg:
    mod = getattr(mod, classname, None)
    inst = getattr(mod, "app", None)

    routes.append(r)
    routes.append(inst)

    return routes


    urls = tuple(auto_add_route())
    app = web.application(urls, globals())
    application = app.wsgifunc()

    if __name__== "__main__":
    app.run()

    </code></pre>
    hayao650
        5
    hayao650  
       2016-01-26 16:12:17 +08:00
    ```python
    ## import apps/目录下所有应用
    def auto_add_route():
    excludes = ".svn common"
    routes = []

    for f in os.listdir(os.getcwd()): ## the same dir with gops.py
    if f in excludes:
    continue

    importstr = ""
    fn = f
    ispkg = True
    if os.path.isdir(f):
    importstr = "from %s import %s" %(f, f)
    else:
    ispkg = False
    if f.endswith('.py'):
    fn, fe = f.split('.')
    importstr = "import %s" %fn

    if len(importstr) == 0:
    continue

    try:
    exec importstr
    except ImportError:
    print "[ERROR] import failed, [%s]" %importstr
    pass

    ## add route
    r = "/myproject/apps/" + fn
    classname = fn
    mod = ""
    try:
    mod = __import__(classname, None, None, [''])
    except ImportError:
    print "[ERROR] import failed, [%s]" % classname
    pass

    if ispkg:
    mod = getattr(mod, classname, None)
    inst = getattr(mod, "app", None)

    routes.append(r)
    routes.append(inst)

    return routes


    urls = tuple(auto_add_route())
    app = web.application(urls, globals())
    application = app.wsgifunc()

    if __name__== "__main__":
    app.run()
    ```
    hayao650
        6
    hayao650  
       2016-01-26 16:13:48 +08:00
    原谅我,我只是想试一下 markdown
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1100 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by slitude
    VERSION: 3.9.8.5 26ms UTC 17:58 PVG 01:58 LAX 09:58 JFK 12:58
    Do have faith in what you're doing.
    ubao msn 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