擅长:python、mysql、java
<blockquote>
<p>I am creating an api in django in which I have a parameter (state_name) and the value of state_name can include <code>'&'</code> in its value which is causing certain problems?</p>
</blockquote>
<p>这是毫无意义的。在查询字符串中,两个参数用与号(<code>&</code>)分隔<em>。例如:</p>
<pre><code>foo=bar&qux=3
</code></pre>
<p>如果您希望<em>内容</em>包含与号,则需要对其进行编码,如:</p>
^{pr2}$
<p>这里<code>%26</code>是与号的编码,然后<em>查询字符串</em>有两个参数:<code>state_name</code>和{<cd4>}。然后解析如下:</p>
<pre><code>>>> from django.http import QueryDict
>>> QueryDict('state_name=jammu%26kashmir&value=2')
<QueryDict: {'state_name': ['jammu&kashmir'], 'value': ['2']}>
</code></pre>
<p>您可以使用<code>QueryDict</code>来构造这样的查询,例如:</p>
<pre><code>>>> qd = QueryDict(mutable=True)
>>> qd['state_name'] = 'jammu&kashmir'
>>> qd['value'] = '2'
>>> qd.urlencode()
'state_name=jammu%26kashmir&value=2'
</code></pre>