我是 Django 的新手,试图让这段代码作为加法器工作。
"""Perform addition using Django"""
template = """
<form method="post" action=" ">{% csrf_token %}
<input type="text" name="a" > + <input type="text" name="b" >
<input type="submit" value="="> <input type="text" name="result >
</form>"""
def add(request):
if request.POST.has_key('a'):
a = int(request.POST['a'])
b = int(request.POST['b'])
result = request.POST['result']
else:
a = 0
b = 0
t = Template(template)
c = RequestContext(request, {'a': a, 'b': b, 'result': a+b})
return HttpResponse(t.render(c))
#url.py
urlpatterns = patterns('',
url(r'^add/$', 'myapp.views.add'),
)
但无法获得正确的结果。不太确定哪里出错了。感谢任何评论