authomatic:不推荐使用REQUEST/返回的状态与存储的状态不匹配 [英] authomatic: REQUEST is deprecated / The returned state doesn't match with the stored state

查看:83
本文介绍了authomatic:不推荐使用REQUEST/返回的状态与存储的状态不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个阻塞问题:我使用的是authomatic 0.1.0,在adapters.py中有一个属性params,其声明如下:

I'm facing a blocking problem: I'm using authomatic 0.1.0 and in adapters.py there's a property params which is declared like this:

@property
def params(self):
    return dict(self.request.REQUEST)

如果让它那样,我会收到此阻止错误:

If I let it like that, I get this blocking error:

Erreur #1 : 'WSGIRequest' object has no attribute 'REQUEST'

在Google搜索中,我的第一个猜测是REQUEST已过时,因此我尝试以这种方式重写它:

Googling for that my first guess is that REQUEST is deprecated, and so I tried to rewrite it this way:

@property
def params(self):
    # (!) Olivier Pons ! REQUEST removed
    a = QueryDict('', mutable=True)
    a.update(self.request.GET)
    a.update(self.request.POST)
    return dict(a.iterlists())

但是我明白了:

Erreur #1 : The returned state "[u'cxxxx']" doesn't match with the stored state!

我不知道自己在做什么错,以及如何正确"替换REQUEST.

I dont know what I'm doing wrong, and how to replace REQUEST "properly".

推荐答案

这是我自己的版本,虽然可能不是最好的版本,但是与v2和v3 python兼容,并解决了iterlists()的主要问题:始终使用此功能返回一个数组,即使只有一个项目也是如此.因此,authomatic会在字符串和具有一个项目的数组"之间进行比较,从而失败.我的解决方案:如果只有一个项目包含一个数组,请保留该项目,而不保留该数组.

Here's my own version, which may not be the best, but is v2 and v3 python compatible, and solves the main problem of iterlists(): this function always returns an array, even where there's only one item. So authomatic makes a comparison between a string and "an array with one item" and thus fails. My solution: if only an array with one item, just keep this item, not the array.

@property
def params(self):
    # (!) Olivier Pons ! REQUEST removed
    a = QueryDict('', mutable=True)
    a.update(self.request.GET)
    a.update(self.request.POST)
    retour = {}
    for key, value in a.iterlists():
        if len(value) > 1:
            retour[key] = value
        else:
            retour[key] = value[0]
    return retour

这篇关于authomatic:不推荐使用REQUEST/返回的状态与存储的状态不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆