Advertisement
joythewell

Adder: revision

Apr 27th, 2012
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. """Perform addition using Django:Revision
  2. """
  3. #{% url add %}pointing to add method by explicitly naming url
  4. template = """<form method="post" action={% url add %}>{% csrf_token %}  
  5.         <input type="text" name="a" value={{ a }} > + <input type="text" name="b" value={{ b }} >
  6.         <input type="submit" value="="> <input type="text" name="result" value={{ result }}>
  7.     </form>"""
  8.  
  9. def add(request):
  10.     try:
  11.         a = int(request.POST.get('a', 0))  #set default to 0 instead of deprecated has_key()   
  12.     except:
  13.         a = 0           # set to 0 if no valid value
  14.     try:
  15.         b = int(request.POST.get('b',0))
  16.     except:
  17.         b = 0
  18.     t = Template (template)
  19.         c = RequestContext(request, {'a': a, 'b': b, 'result': a+b})
  20.     return HttpResponse(t.render(c))
  21.  
  22. #url.py
  23. urlpatterns = patterns('',     
  24.     url(r'^add/$', 'myapp.views.add', name='add'),  
  25. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement