Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我可以使用GAE+;python 2.7+;django.utils.translation?_Python_Django_Google App Engine_Localization_Internationalization - Fatal编程技术网

我可以使用GAE+;python 2.7+;django.utils.translation?

我可以使用GAE+;python 2.7+;django.utils.translation?,python,django,google-app-engine,localization,internationalization,Python,Django,Google App Engine,Localization,Internationalization,我使用i18n进行本地化,就像django使用自定义请求处理程序在不同语言之间切换并升级到python 2.7一样,这会带来麻烦,因为它使用了来自django.utils.translation的cookie。在最坏的情况下,我必须从我们的自定义请求处理程序转移到其他可以为我们切换语言的系统。你知道如何解决这个错误吗?django语言cookie是否仍然像以前一样可用?我还有哪些不依赖django的本地化替代方案?巴贝尔?我还可以使用旧的.po和.mo文件吗?我应该使用其他Cookie类吗?我的

我使用i18n进行本地化,就像django使用自定义请求处理程序在不同语言之间切换并升级到python 2.7一样,这会带来麻烦,因为它使用了来自
django.utils.translation
的cookie。在最坏的情况下,我必须从我们的自定义请求处理程序转移到其他可以为我们切换语言的系统。你知道如何解决这个错误吗?django语言cookie是否仍然像以前一样可用?我还有哪些不依赖django的本地化替代方案?巴贝尔?我还可以使用旧的.po和.mo文件吗?我应该使用其他Cookie类吗?我的自定义请求处理程序类:

from django.utils import translation

class I18NHandler(webapp.RequestHandler):   

    def initialize(self, request, response):
        webapp.RequestHandler.initialize(self, request, response)
        self.request.COOKIES = Cookies(self)
        self.request.META = os.environ
        self.reset_language()

    def reset_language(self):

        # Decide the language from Cookies/Headers

        language = translation.get_language_from_request(self.request)
        translation.activate(language)
        self.request.LANGUAGE_CODE = translation.get_language()

        # Set headers in response

        self.response.headers['Content-Language'] = \
            translation.get_language()


class Cookies(UserDict.DictMixin):

    def __init__(self, handler, **policy):
        self.response = handler.response
        self._in = handler.request.cookies
        self.policy = policy
        if 'secure' not in policy \
            and handler.request.environ.get('HTTPS', '').lower() \
            in ['on', 'true']:
            policy['secure'] = True
        self._out = {}

    def __getitem__(self, key):
        if key in self._out:
            return self._out[key]
        if key in self._in:
            return self._in[key]
        raise KeyError(key)

    def __setitem__(self, key, item):
        self._out[key] = item
        self.set_cookie(key, item, **self.policy)

    def __contains__(self, key):
        return key in self._in or key in self._out

    def keys(self):
        return self._in.keys() + self._out.keys()

    def __delitem__(self, key):
        if key in self._out:
            del self._out[key]
            self.unset_cookie(key)
        if key in self._in:
            del self._in[key]
            p = {}
            if 'path' in self.policy:
                p['path'] = self.policy['path']
            if 'domain' in self.policy:
                p['domain'] = self.policy['domain']
            self.delete_cookie(key, **p)

    # begin WebOb functions

    def set_cookie(
        self,
        key,
        value='',
        max_age=None,
        path='/',
        domain=None,
        secure=None,
        httponly=False,
        version=None,
        comment=None,
        ):
        """
        Set (add) a cookie for the response
        """

        cookies = BaseCookie()
        cookies[key] = value
        for (var_name, var_value) in [
            ('max-age', max_age),
            ('path', path),
            ('domain', domain),
            ('secure', secure),
            ('HttpOnly', httponly),
            ('version', version),
            ('comment', comment),
            ]:
            if var_value is not None and var_value is not False:
                cookies[key][var_name] = str(var_value)
            if max_age is not None:
                cookies[key]['expires'] = max_age
        header_value = cookies[key].output(header='').lstrip()
        self.response.headers._headers.append(('Set-Cookie',
                header_value))

    def delete_cookie(
        self,
        key,
        path='/',
        domain=None,
        ):
        """
        Delete a cookie from the client.  Note that path and domain must match
        how the cookie was originally set.
        This sets the cookie to the empty string, and max_age=0 so
        that it should expire immediately.
        """

        self.set_cookie(key, '', path=path, domain=domain, max_age=0)

    def unset_cookie(self, key):
        """
        Unset a cookie with the given name (remove it from the
        response).  If there are multiple cookies (e.g., two cookies
        with the same name and different paths or domains), all such
        cookies will be deleted.
        """

        existing = self.response.headers.get_all('Set-Cookie')
        if not existing:
            raise KeyError('No cookies at all have been set')
        del self.response.headers['Set-Cookie']
        found = False
        for header in existing:
            cookies = BaseCookie()
            cookies.load(header)
            if key in cookies:
                found = True
                del cookies[key]
            header = cookies.output(header='').lstrip()
            if header:
                self.response.headers.add('Set-Cookie', header)
        if not found:
            raise KeyError('No cookie has been set with the name %r'
                           % key)
错误 跟踪:


提前感谢您的建议。

我相信这是因为WebOb已经用新的Python 2.7运行时进行了升级

你应使用:

res.headers.add('Set-Cookie', header_value)
正如webob中指出的那样


请注意,webapp也被替换为。

谢谢,我将按照这些说明进行操作。
res.headers.add('Set-Cookie', header_value)