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
jxwho
V2EX  ›  Python

flask 设计 API

  •  1
     
  •   jxwho · 2014-06-14 14:58:56 +08:00 · 5919 次点击
    这是一个创建于 3598 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在flask中,一般都是这样:

    @app.route('/logIn')
    def logIn():
    ...
    return render_template('...')


    假如我在移动端访问这个路径,那就会返回 render 出来的东西,那应该怎么设计才能更好地把这两者分开, 使得web跟mobile都可以用?
    19 条回复    2014-07-16 16:51:32 +08:00
    hepochen
        1
    hepochen  
       2014-06-14 15:08:48 +08:00 via iPhone
    headers判断
    moroumo
        2
    moroumo  
       2014-06-14 15:14:31 +08:00
    应该用响应式设计去处理页面。
    不过看你的想法不同的UA redern不同的template。
    http://stackoverflow.com/questions/9878020/how-do-i-get-the-user-agent-with-flask
    jxwho
        3
    jxwho  
    OP
       2014-06-14 15:17:27 +08:00
    @moroumo 我表达得好像不是很清楚,在手机端的话就是只是通过这些API来获取数据或者做判断,不需要返回html.
    hepochen
        4
    hepochen  
       2014-06-14 15:20:27 +08:00 via iPhone
    @jxwho 仔细看别人的建议,包括后半句
    yelite
        5
    yelite  
       2014-06-14 15:22:04 +08:00
    判断 header,移动端只是要获取数据的话就设置 request 的 header
    Accept: application/json
    yangzh
        6
    yangzh  
       2014-06-14 15:32:52 +08:00
    @jxwho

    要不就用 header 判断,if else 返回不同的东西。

    要不就用一个新路由
    @app.route('/api/logIn')
    def api_logIn(): reurn jsonify('...')


    我个人觉得加多一个新路由更加好吧。
    tokki
        7
    tokki  
       2014-06-14 16:34:42 +08:00
    重新写个函数类似
    render(tpl):
    tokki
        8
    tokki  
       2014-06-14 16:39:06 +08:00
    render(tpl):
    if mobile:
    return render_template("mobile/“+tpl)
    else
    return render_template(tpl)
    Livid
        9
    Livid  
    MOD
       2014-06-14 16:43:25 +08:00
    V2EX 在 Tornado 里用的也是类似 8 楼 @tokki 这样的解决方案。

    并且,还加入了对 mobile tpl 是否存在的判断,如果不存在的话,那就还是渲染桌面版。
    billlee
        10
    billlee  
       2014-06-14 16:57:07 +08:00
    功能不同的接口就不要用同一个名称,把 API 放到 /api/login
    ccbikai
        11
    ccbikai  
       2014-06-14 18:01:08 +08:00
    要做API就做API,要做网页就做网页,不要搞到一个路由里。

    判断移动端,就处理一下UA,判断UA是否是移动设备
    if mobile:
    return "mobiile"
    else:
    return "desktop"
    jxwho
        12
    jxwho  
    OP
       2014-06-14 19:05:49 +08:00
    @moroumo
    @hepochen
    @yelite
    @yangzh
    @tokki
    @Livid
    @billlee
    @ccbikai
    感谢各位大大!
    cbsw
        13
    cbsw  
       2014-06-14 20:42:05 +08:00   ❤️ 1
    这样的需求是不是应该考虑 RESTful,而且有 http://flask-restful.readthedocs.org/en/latest/ 这个现成的库可以用
    jxwho
        14
    jxwho  
    OP
       2014-06-14 20:47:15 +08:00
    @cbsw 对的,我想过restful,但是,,,是不是这样就要做两次请求?
    cbsw
        15
    cbsw  
       2014-06-14 20:51:08 +08:00
    @jxwho 一套统一的 API,然后不同的应用有各自的渲染方案,直接通过 url 对数据进行操作。不知你说的两次请求是什么意思
    jxwho
        16
    jxwho  
    OP
       2014-06-14 20:53:36 +08:00
    @cbsw 比如说,我有一个post资源,
    然后 index 页面要展示这些 post 的话,是不是用户访问 /index,然后处理函数再请求post的get?
    然后 如果 mobile的话,可以直接 /post/get 这样?
    cbsw
        17
    cbsw  
       2014-06-14 21:09:28 +08:00
    @jxwho web 端用 restful 的话,大部分操作都要通过异步 ajax 实现,应用可以直接是一个静态页面,具体可以参考 https://github.com/miguelgrinberg/REST-tutorial ,另外玩 flask 可以关注一下 miguelgrinberg 这个人,博客(http://blog.miguelgrinberg.com/)里有很多非常好的 tutorial
    jxwho
        18
    jxwho  
    OP
       2014-06-14 22:35:48 +08:00
    @cbsw soga, thank you!
    zjnjxufe
        19
    zjnjxufe  
       2014-07-16 16:51:32 +08:00
    if request.user_agent.platform in ('android', 'iphone'):
    return xxx
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2685 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 15:17 · PVG 23:17 · LAX 08:17 · JFK 11:17
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.