Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Perform addition using Django:Revision
- """
- #{% url add %}pointing to add method by explicitly naming url
- template = """<form method="post" action={% url add %}>{% csrf_token %}
- <input type="text" name="a" value={{ a }} > + <input type="text" name="b" value={{ b }} >
- <input type="submit" value="="> <input type="text" name="result" value={{ result }}>
- </form>"""
- def add(request):
- try:
- a = int(request.POST.get('a', 0)) #set default to 0 instead of deprecated has_key()
- except:
- a = 0 # set to 0 if no valid value
- try:
- b = int(request.POST.get('b',0))
- except:
- 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', name='add'),
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement