推荐学习书目
› 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 · Oct 2, 2012 · 7113 views
    This topic created in 5112 days ago, the information mentioned may be changed or developed.
    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 replies  •  1970-01-01 08:00:00 +08:00
    fire5
        1
    fire5  
    OP
       Oct 2, 2012
    如果获取失败会抛出异常,是指 表单中的元素 没有value。
    fire5
        2
    fire5  
    OP
       Oct 2, 2012
    在问一个问题 {{ message }} 如果没有设置 将会出现 gloable namespce ’message‘ not define 的异常,
    这合理吗?
    fire5
        3
    fire5  
    OP
       Oct 2, 2012
    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
       Oct 2, 2012
    相当土鳖啊
    fire5
        5
    fire5  
    OP
       Oct 2, 2012
    请问 有什么好的 解决方法吗?
    messense
        6
    messense  
       Oct 2, 2012   ❤️ 1
    @fire5 调用 self.render 的时候不论提交没提交 POST 都把要传入模板的变量传入,初始值为空呗。
    fire5
        7
    fire5  
    OP
       Oct 2, 2012
    嗯 我也是这么做了 不过感觉还是很土鳖啊
    如果 这样
    {% if message %}
    {{ message }}
    {% end %}
    如果传入模版中没有 则为None,然后用 if 做处理 感觉会平滑一点
    fire5
        8
    fire5  
    OP
       Oct 2, 2012
    感谢 @messense
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1399 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:19 · PVG 00:19 · LAX 09:19 · JFK 12:19
    ♥ Do have faith in what you're doing.