- views.py
employee_list = Employee.objects.filter(dept_code__startswith=data_dict[0])
print(type(data_dict))
print('dept_code:',data_dict[0])
print(employee_list)
需要将参数重新转化为字典。simplejson
import simplejson
# loads 把字符串转换成字典
#{}里都是双引号,会成功
str_dic = '{"name":"zeropython","age":"testa"}'
new_dic = simplejson.loads(str_dic)
#{}里都是单引号,会报错
str_dic = "{'name':'zeropython','age':'testa'}"
#new_dic = simplejson.loads(str_dic)
#simplejson.errors.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
#{}里都是单引号,需要转化
str_dic = "{'name':'zeropython','age':'testa'}"
str_dic = str_dic.replace('"','|').replace("'",'"').replace('|','"')
new_dic = simplejson.loads(str_dic)
print(type(new_dic))
链接
- 实际上json格式的引号需要为双引号,否则会报错如下。str_dic = "{‘name’:‘zeropython’,‘age’:‘testa’}"会报错
- 故需要替换单引号为双引号。



