def u_query(request): user_permission = query() return render(request,'query.html',locals())
这样写前台页面显示无法解码
TemplateSyntaxError at /query/
Could not parse the remainder: '['name']' from 'user_per['name']'
Request Method: GET
Request URL: http://127.0.0.1:8000/query/
Django Version: 1.9.4
Exception Type: TemplateSyntaxError
Exception Value:
Could not parse the remainder: '['name']' from 'user_per['name']'
换用Httpresponse之后
def u_query(request): user_permission = query() # return HttpResponse(user_permission) return render(request,'query.html',locals())
前端页面显示出要查询的内容
{'user_code': 444, 'user_name': u'\u7b2c4\u4e2a'}{'user_code': 555, 'user_name': u'\u7b2c5\u4e2a'}{'user_code': 22, 'user_name': u'22'}{'user_code': 11, 'user_name': u'2'}{'user_code': 234, 'user_name': u'2324'}
返回的user_permission是这样查询得到的
def query(): result = [] s = select([users.c.name,users.c.code,]) c = engine.execute(s) for row in c: info = {} info['user_name'] = row[0] info['user_code'] = row[1] result.append(info) c.close() return result
返回是一个list,每个元素是一个dict
请问render为什么会报这样的错误?
用这个
return render_to_response('query.html', {'user_permission': user_permission},context_instance=RequestContext(request))
然后在query.html里可以循环user_permission
{% for user in user_permission%}
{{ user.user_name }}
{% endfor %}