简体   繁体   English

Django 中的查询字典

[英]QueryDict in Django

from django.http import QueryDict
QueryDict('query=c++')

Why the output is, <QueryDict: {u'query': [u'c ']}>为什么 output 是, <QueryDict: {u'query': [u'c ']}>

Does querydict remove the + symbol from the string? querydict 是否从字符串中删除 + 符号? I am using Python 2.7.17 and Django version 1.10.4.我正在使用 Python 2.7.17 和 Django 版本 1.10.4。

When you instantiate QueryDict class, it invoke parse_qs to parse the query_string (In this case it's 'query=c++' ).当您实例化QueryDict class 时,它调用parse_qs来解析query_string (在本例中为'query=c++' )。 See the django QueryDict implementation here .请参阅此处的 django QueryDict实现。

>>> from urllib.parse import parse_qsl
>>> parse_qsl("query=c++")
[('query', 'c  ')]

So why the result has space instead of + ?.那么为什么结果有空格而不是+ Well the answer is within the query string, the plus sign is reserved as shorthand notation for a space .答案在查询字符串中,加号保留为空格的简写符号 Therefore, real plus signs must be encoded.因此,必须对真正的加号进行编码。

>>> from urllib.parse import parse_qsl
>>> parse_qsl("query=c%2B%2B")
[('query', 'c++')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM