Django Book 翻译
菜单>:
TOC
返回
原文:
Note that were using the ``get()`` method on ``request.GET`` , which is potentially confusing. The ``get()`` method here is the one that every Python dictionary has. Were using it here to be careful: it is *not* safe to assume that ``request.GET`` contains a ``'q'`` key, so we use ``get('q', '')`` to provide a default fallback value of ``''`` (the empty string). If we merely accessed the variable using ``request.GET['q']`` , that code would raise a ``KeyError`` if ``q`` wasnt available in the GET data.
翻译:
注意在 ``request.GET`` 中使用了 ``get()`` 方法,这可能让大家不好理解。这里的 ``get()`` 是每个python的的字典数据类型都有的方法。使用的时候要小心:假设 ``request.GET`` 包含一个 ``'q'`` 的key是不安全的,所以我们使用 ``get('q', '')`` 提供一个缺省的返回值 ``''`` (一个空字符串)。如果只是使用 ``request.GET['q']`` 访问变量,在Get数据时 ``q`` 不可得,可能引发 ``KeyError`` .
备注:
译者: