Py学习  »  问与答

运行django出错,object has no attribute 'has_key',是什么问题?

quyip • 10 年前 • 7560 次点击  

源码是

from django.http import HttpResponse
text = """<form method="post" action="/add/">
    <input type="text" name="a" value="%d"> + <input type="text" name="b" value="%d">
    <input type="submit" value="="> <input type="text" value="%d">
</form>"""
def index(request):
    if request.POST.has_key('a'):
        a = int(request.POST['a'])
        b = int(request.POST['b'])
    else:
        a = 0
        b = 0
    return HttpResponse(text % (a, b, a + b))

出错是 'QueryDict' object has no attribute 'has_key'

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/377
 
7560 次点击  
文章 [ 2 ]  |  最新文章 10 年前
python自学男
Reply   •   1 楼
python自学男    10 年前

查询的query对象没有这个属性

木头lbj
Reply   •   2 楼
木头lbj    10 年前

没有has_key属性。
用:

if 'a' in request.POST:  
    a = .......