在会话中存储 QueryDict 后无法进行 urlencode()
我尝试将其发布到 django-users 组 ( http://groups. google.com/group/django-users/browse_thread/thread/8572d7f4075cfe0e )但没有回应。也许在这里我会得到更多的帮助。
我将 request.GET
存储在会话中:
request.session['query_string'] = request.GET
然后我在另一个页面中检索该值并尝试对该值进行 urlencode QueryDict:
context['query_string'] = request.session['query_string'].urlencode()
在我的上下文中,我得到了 python 的字符串表示形式 QueryDict 对象而不是预期的 key0=value0&key1=value1&...
细绳。
如果我将 urlencoded 字符串存储在 会话,当然一切正常:
request.session['query_string'] = request.GET.urlencode()
这是一个错误吗?
I tried to post this to django-users group ( http://groups.google.com/group/django-users/browse_thread/thread/8572d7f4075cfe0e ) but got no responses. Maybe here I will get more help.
I store request.GET
in session:
request.session['query_string'] = request.GET
then I retrieve the value in another page and try to urlencode the
QueryDict:
context['query_string'] = request.session['query_string'].urlencode()
in my context I get the python's string representation of the
QueryDict object instead of the expected key0=value0&key1=value1&...
string.
If, instead of QueryDict, I store the urlencoded string in the
session, everything works of course:
request.session['query_string'] = request.GET.urlencode()
is it a bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是很古老的了,但是当我因为同样的问题而绊倒自己时,我想为那些通过搜索达到这个问题的人提供一个解决方案。核心问题是Django使用的默认序列化器会话数据:
django.contrib.sessions.serializers.JSONSerializer
。此序列化程序不知道与多个值关联的键(对于它们,应使用 QueryDict 的 getlist() 方法),这只会导致每个值的最后一个值正在使用的密钥。有两种可能的解决方案:
request.GET
或request.POST
对象从会话中检索;QueryDict
的__getstate__()
方法,另请参阅旧 Django 票证。This is very old, but as I stumbled myself because of the same issue, I want to offer a solution for those reaching this question via a search. The core issue is the default serializer Django uses for the session data:
django.contrib.sessions.serializers.JSONSerializer
. This serializer is not aware of keys associated with multiple values (for them, thegetlist()
method ofQueryDict
should be used), which results in only the last value for each key being used.There are two possible solutions:
request.GET
orrequest.POST
object when storing & retrieving from a session;__getstate__()
method ofQueryDict
, see also an old Django ticket.django.contrib.sessions.serializers.PickleSerializer
.这不是一个错误。如果您查看 QueryDict 定义(请参阅 https://github.com/django/django/blob/master/django/http/init.py),它明确表示它是不可变的,除非您创建副本它的。
为了演示这一点,这是我在 Python shell 中的内容,
默认情况下,
mutable
参数设置为 False,并且由于request.session['query_string'] = request.GET
首先将其初始化为一个空的 QueryDict,调用urlencode()
仅返回一个空 str,而request.session['query_string'] = request.GET.urlencode()
之所以有效,是因为您正在使用已使用适当的键/值初始化的 QueryDict。This is not a bug. If you take a peek at the
QueryDict
definition (see https://github.com/django/django/blob/master/django/http/init.py), it says explicitly that it's immutable unless you create a copy of it.To demonstrate this, here's what I have in my Python shell,
The
mutable
argument is set to False by default, and sincerequest.session['query_string'] = request.GET
initialized it to an empty QueryDict to begin with, callingurlencode()
only returns you an empty str while therequest.session['query_string'] = request.GET.urlencode()
works because you're working with a QueryDict that has been initialized with the appropriate key/values.