Attachment 'views.py'

Download

   1 from django.http import HttpResponse
   2 from django.template import Context,Template,loader
   3 
   4 def chyba(text):
   5 
   6     t=loader.get_template("chyba.html")
   7     c=Context({'chyba':text})
   8     return HttpResponse(t.render(c))
   9 
  10 class Dvojica(object):
  11 
  12     def __init__(self,n,hodnota):
  13         self.n = n
  14         self.hodnota = hodnota
  15 
  16 
  17 def nasobky(request,cislo):
  18 
  19     zoz_nasobkov=[]
  20     # cislo je retazec
  21     # musime ho previest na int
  22     # treba kontrolovat vstup uzivatela! 
  23     try:
  24         cislo_int=int(cislo)
  25     except ValueError:
  26         return chyba("%s sa nepodarilo previest na cislo" % cislo)
  27     if cislo_int<0:
  28         return chyba("cislo %d je zaporne" % cislo_int)
  29     for i in range(11):
  30         zoz_nasobkov.append(Dvojica(i,i*cislo_int))
  31     c=Context({'cislo':cislo_int,'vysledky':zoz_nasobkov})
  32     t=loader.get_template("nasobky.html")
  33     return HttpResponse(t.render(c))
  34 
  35 # Predosla a nasledujuca funkcia
  36 # obsahuju zjavne duplicitny kod.
  37 # To je bezzasadove, ale nechcem veci komplikovat.
  38 
  39 
  40 def delitele(request,cislo):
  41 
  42     zoz_delitelov=[]
  43     # cislo je retazec
  44     # musime ho previest na int
  45     # treba kontrolovat vstup uzivatela! 
  46     try:
  47         cislo_int=int(cislo)
  48     except ValueError:
  49         return chyba("%s sa nepodarilo previest na cislo" % cislo)
  50     if cislo_int<0:
  51         return chyba("cislo %d je zaporne" % cislo_int)
  52     pocitadlo=0
  53     for i in range(1,cislo_int+1):
  54         if (cislo_int % i)==0:
  55             pocitadlo+=1
  56             zoz_delitelov.append(Dvojica(pocitadlo,i))
  57     c=Context({'cislo':cislo_int,'vysledky':zoz_delitelov})
  58     t=loader.get_template("delitele.html")
  59     return HttpResponse(t.render(c))

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.