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

tornado get_argument 代码

  •  
  •   fire5 · 2012-10-02 15:28:40 +08:00 · 5954 次点击
    这是一个创建于 4223 天前的主题,其中的信息可能已经有所发展或是发生改变。
    def get_argument(self, name, default=_ARG_DEFAULT, strip=True):
    """Returns the value of the argument with the given name.

    If default is not provided, the argument is considered to be
    required, and we throw an HTTP 400 exception if it is missing.

    If the argument appears in the url more than once, we return the
    last value.

    The returned value is always unicode.
    """
    args = self.get_arguments(name, strip=strip)
    if not args:
    if default is self._ARG_DEFAULT:
    raise HTTPError(400, "Missing argument %s" % name)
    return default
    return args[-1]

    这个如果不设默认值,如果获取失败会抛出异常,感觉合理吗?
    8 条回复    1970-01-01 08:00:00 +08:00
    fire5
        1
    fire5  
    OP
       2012-10-02 15:31:04 +08:00
    如果获取失败会抛出异常,是指 表单中的元素 没有value。
    fire5
        2
    fire5  
    OP
       2012-10-02 15:32:21 +08:00
    在问一个问题 {{ message }} 如果没有设置 将会出现 gloable namespce ’message‘ not define 的异常,
    这合理吗?
    fire5
        3
    fire5  
    OP
       2012-10-02 18:24:02 +08:00
    stack overfollow 给出的答案:
    {% try %}
    {% if message %}
    Oops! The logout failed. Please close all open documents and try again
    {% end %}
    {% except %}
    {% end %}


    http://stackoverflow.com/questions/10139341/handling-an-undefined-template-variable-in-tornado
    fire5
        4
    fire5  
    OP
       2012-10-02 18:24:20 +08:00
    相当土鳖啊
    fire5
        5
    fire5  
    OP
       2012-10-02 19:08:02 +08:00
    请问 有什么好的 解决方法吗?
    messense
        6
    messense  
       2012-10-02 21:06:26 +08:00   ❤️ 1
    @fire5 调用 self.render 的时候不论提交没提交 POST 都把要传入模板的变量传入,初始值为空呗。
    fire5
        7
    fire5  
    OP
       2012-10-02 21:59:10 +08:00
    嗯 我也是这么做了 不过感觉还是很土鳖啊
    如果 这样
    {% if message %}
    {{ message }}
    {% end %}
    如果传入模版中没有 则为None,然后用 if 做处理 感觉会平滑一点
    fire5
        8
    fire5  
    OP
       2012-10-02 22:00:03 +08:00
    感谢 @messense
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1811 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:35 · PVG 00:35 · LAX 09:35 · JFK 12:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.