Django:我可以从字典创建一个QueryDict吗? [英] Django: Can I create a QueryDict from a dictionary?

查看:625
本文介绍了Django:我可以从字典创建一个QueryDict吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下,我的Django应用程序中有一个字典:

Imagine that I have a dictionary in my Django application:

dict = {'a': 'one', 'b': 'two', }

现在我想轻松地创建一个urlencoded的GET参数列表这个字典。当然,我可以循环通过字典,urlencode键和值,然后自己连接字符串,但必须有一个更简单的方法。我想使用一个QueryDict实例。 QueryDict是dict的一个子类,所以它应该是可能的。

Now I want to easily create an urlencoded list of GET parameters from this dictionary. Of course I could loop through the dictionary, urlencode keys and values and then concatenate the string by myself, but there must be an easier way. I would like to use a QueryDict instance. QueryDict is a subclass of dict, so it should be possible somehow.

qdict = QueryDict(dict) # this does not actually work
print qdict.urlencode()

我将如何做第二个最后一行工作?

How would I make the second to last line work?

推荐答案

如何?

from django.http import QueryDict
dict = {'a': 'one', 'b': 'two', }
qdict = QueryDict('', mutable=True)
qdict.update(dict)

这篇关于Django:我可以从字典创建一个QueryDict吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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