使用时
request.environ['QUERY_STRING']
我正在获得价值ugq_no=2
,但我怎样才能获得 2 的价值?
参考代码:
def get_ugall_record(request):
qry_obj = db.Query(user_guide).order('ugq_no')
all_list = []
for q in qry_obj:
all_list.append(q)
return HttpResponse(serializers.serialize('json', all_list), mimetype='application/json')
def get_faqall_record(request):
qry_obj = db.Query(faqs).order('faq_no')
all_list = []
for q in qry_obj:
all_list.append(q)
return HttpResponse(serializers.serialize('json', all_list), mimetype='application/json')
我想在一个函数中创建这两个函数。
所以我正在尝试:
def get_ugall_record(request):
logging.debug('----1-----')
logging.debug(request.GET.get('ugq_no'))
if request.GET.get('ugq_no') != 'None':
logging.debug('----2-----')
ug_qry_obj = db.Query(user_guide).order('ugq_no')
ug_all_list = []
for q in ug_qry_obj:
ug_all_list.append(q)
return HttpResponse(serializers.serialize('json', ug_all_list), mimetype='application/json')
elif request.GET.get('faq_no') != 'None':
logging.debug('----3-----')
qry_obj = db.Query(faqs).order('faq_no')
all_list = []
for q in qry_obj:
all_list.append(q)
return HttpResponse(serializers.serialize('json', all_list), mimetype='application/json')
但这不是elif request.GET.get('faq_no')!='无' 可以做些什么?
答案 0 :(得分:1)
>>> import urlparse
>>> formdata = urlparse.parse_qs('ugq_no=2')
>>> formdata
{'ugq_no': ['2']}
>>> formdata['ugq_no']
['2']
>>> formdata['ugq_no'][0]
'2'
在Python 3.x中,使用urllib.parse.parse_qs
:
>>> import urllib.parse
>>> formdata = urllib.parse.parse_qs('ugq_no=2')
>>> formdata
{'ugq_no': ['2']}
>>> formdata['ugq_no']
['2']
>>> formdata['ugq_no'][0]
'2'
或使用request.GET
:
value = request.GET.get('ugq_no')
<强>更新强>
def get_ugall_record(request):
if 'ugq_no' in request.GET:
qry_obj = db.Query(user_guide).order('ugq_no')
return qry_response(qry_obj)
elif 'faq_no' in request.GET:
qry_obj = db.Query(faqs).order('faq_no')
return qry_response(qry_obj)
def qry_response(qry_obj):
all_list = []
for q in qry_obj:
all_list.append(q)
return HttpResponse(serializers.serialize('json', all_list), mimetype='application/json')