examples.html 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. {{extend 'layout.html'}}
  2. {{import os}}
  3. <div class="onecolcontent">
  4. <h2>web2py<sup style="font-size:0.5em;">TM</sup> Examples</h2>
  5. <div id="navigation">
  6. <a href="#simple_examples">simple</a> |
  7. <a href="#session_examples">session</a> |
  8. <a href="#template_examples">template</a> |
  9. <a href="#layout_examples">layout</a> |
  10. <a href="#form_examples">form</a> |
  11. <a href="#database_examples">database</a> |
  12. <a href="#cache_examples">cache</a> |
  13. <a href="#ajax_examples">ajax</a> |
  14. <a href="#testing_examples">testing</a> |
  15. <a href="#streaming_examples">streaming</a> |
  16. <a href="#xmlrpc_examples">xmlrpc</a> |
  17. <a href="http://www.web2py.com/book/default/chapter/06">dal</a> |
  18. <a href="http://www.web2py.com/book/default/chapter/07">crud</a> |
  19. <a href="http://www.web2py.com/book/default/chapter/08">auth</a>
  20. </div>
  21. <div id="scrollhere">
  22. <h2 id="simple_examples">Simple Examples</h2>
  23. <p><i>Here are some working and complete examples that explain the basic syntax of the framework.<br/>
  24. You can click on the web2py keywords (in the highlighted code!) to get documentation.</i></p>
  25. <h3>Example {{c=1}}{{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  26. {{=CODE("""
  27. def hello1():
  28. return "Hello World"
  29. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  30. <p>If the controller function returns a string, that is the body of the rendered page.<br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/hello1">hello1</a></p>
  31. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  32. {{=CODE("""
  33. def hello2():
  34. return T("Hello World")
  35. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  36. <p>The function T() marks strings that need to be translated. Translation dictionaries can be created at /admin/default/design<br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/hello2">hello2</a></p>
  37. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  38. {{=CODE("""
  39. def hello3():
  40. return dict(message=T("Hello World"))
  41. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  42. <b>and view: simple_examples/hello3.html</b>
  43. {{=CODE(open(os.path.join(request.folder,'views/simple_examples/hello3.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  44. <p>If you return a dictionary, the variables defined in the dictionery are visible to the view (template).
  45. <br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/hello3.html">hello3</a></p>
  46. <p>Actions can also be be rendered in other formsts like JSON, <a href="/{{=request.application}}/simple_examples/hello3.json">hello3.json</a>, and XML, <a href="/{{=request.application}}/simple_examples/hello3.xml">hello3.xml</a></p>
  47. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  48. {{=CODE("""
  49. def hello4():
  50. response.view='simple_examples/hello3.html'
  51. return dict(message=T("Hello World"))
  52. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  53. <p>You can change the view, but the default is /[controller]/[function].html. If the default is not found web2py tries to render the page using the generic.html view.
  54. <br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/hello4">hello4</a></p>
  55. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  56. {{=CODE("""
  57. def hello5():
  58. return HTML(BODY(H1(T('Hello World'),_style="color: red;"))).xml() # .xml to serialize
  59. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  60. <p>You can also generate HTML using helper objects HTML, BODY, H1, etc. Each of these tags is a class and the views know how to render the corresponding objects. The method .xml() serializes them and produce html/xml code for the page.
  61. Each tag, DIV for example, takes three types of arguments:</p>
  62. <ul>
  63. <li>unnamed arguments, they correspond to nested tags</li>
  64. <li>named arguments and name starts with '_'. These are mapped blindly into tag attributes and the '_' is removed. attributes without value like "READONLY" can be created with the argument "_readonly=ON".</li>
  65. <li>named arguments and name does not start with '_'. They have a special meaning. See "value=" for INPUT, TEXTAREA, SELECT tags later.
  66. </ul>
  67. <p>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/hello5">hello5</a></p>
  68. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  69. {{=CODE("""
  70. def hello6():
  71. response.flash=T("Hello World in a flash!")
  72. return dict(message=T("Hello World"))
  73. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  74. <p>response.flash allows you to flash a message to the user when the page is returned. Use session.flash instead of response.flash to display a message after redirection. With default layout, you can click on the flash to make it disappear.
  75. <br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/hello6">hello6</a></p>
  76. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  77. {{=CODE("""
  78. def status():
  79. return dict(toobar=response.toolbar())
  80. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  81. <p>Here we are showing the request, session and response objects using the generic.html template.
  82. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  83. {{=CODE("""
  84. def redirectme():
  85. redirect(URL('hello3'))
  86. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  87. <p>You can do redirect.
  88. <br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/redirectme">redirectme</a></p>
  89. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  90. {{=CODE("""
  91. def raisehttp():
  92. raise HTTP(400,"internal error")
  93. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  94. <p>You can raise HTTP exceptions to return an error page.
  95. <br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/raisehttp">raisehttp</a></p>
  96. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  97. {{=CODE("""
  98. def raiseexception():
  99. 1/0
  100. return 'oops'
  101. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  102. <p>If an exception occurs (other than HTTP) a ticket is generated and the event is logged for the administrator. These tickets and logs can be accessed, reviewed and deleted at any later time.
  103. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  104. {{=CODE("""
  105. def servejs():
  106. import gluon.contenttype
  107. response.headers['Content-Type']=gluon.contenttype.contenttype('.js')
  108. return 'alert("This is a Javascript document, it is not supposed to run!");'
  109. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  110. <p>You can serve other than HTML pages by changing the contenttype via the response.headers. The gluon.contenttype module can help you figure the type of the file to be served. NOTICE: this is not necessary for static files unless you want to require authorization.
  111. <br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/servejs">servejs</a></p>
  112. <h3 id="example_json">Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  113. {{=CODE("""
  114. def makejson():
  115. return response.json(['foo', {'bar': ('baz', None, 1.0, 2)}])
  116. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  117. <p>If you are into Ajax, <a href="http://www.json.org">JSON</a> is fully supported in web2py. Providing a fast and easy way to serve asynchronous content to your Ajax page. Response.json can serialize most Python types into JSON.
  118. <br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/makejson">makejson</a></p>
  119. <p>New in web2py 1.63: Any normal action returning a dict is automatically serialized in JSON if '.json' is appended to the URL.</p>
  120. <h3 id="example_rtf">Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  121. {{=CODE("""
  122. def makertf():
  123. import gluon.contrib.pyrtf as q
  124. doc=q.Document()
  125. section=q.Section()
  126. doc.Sections.append(section)
  127. section.append('Section Title')
  128. section.append('web2py is great. '*100)
  129. response.headers['Content-Type']='text/rtf'
  130. return q.dumps(doc)
  131. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  132. <p>web2py also includes gluon.contrib.<a href="http://pyrtf.sourceforge.net/">pyrtf</a>, developed by Simon Cusack and revised by Grant Edwards. This module allows you to generate Rich Text Format documents including colored formatted text and pictures.<br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/makertf">makertf</a></p>
  133. <h3 id="example_rss">Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  134. {{=CODE("""
  135. def rss_aggregator():
  136. import datetime
  137. import gluon.contrib.rss2 as rss2
  138. import gluon.contrib.feedparser as feedparser
  139. d = feedparser.parse("http://rss.slashdot.org/Slashdot/slashdot/to")
  140. rss = rss2.RSS2(title=d.channel.title,
  141. link = d.channel.link,
  142. description = d.channel.description,
  143. lastBuildDate = datetime.datetime.now(),
  144. items = [
  145. rss2.RSSItem(
  146. title = entry.title,
  147. link = entry.link,
  148. description = entry.description,
  149. # guid = rss2.Guid('unkown'),
  150. pubDate = datetime.datetime.now()) for entry in d.entries]
  151. )
  152. response.headers['Content-Type']='application/rss+xml'
  153. return rss2.dumps(rss)
  154. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  155. <p>web2py includes gluon.contrib.<a href="http://www.dalkescientific.com/Python/PyRSS2Gen.html">rss2</a>, developed by Dalke Scientific Software, which generates RSS2 feeds, and
  156. gluon.contrib.<a href="http://www.feedparser.org/">feedparser</a>, developed by Mark Pilgrim, which collects RSS and ATOM feeds. The above controller collects a slashdot feed and makes new one.
  157. <br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/rss_aggregator">rss_aggregator</a></p>
  158. <h3 id="example_wiki">Example {{=c}}{{c+=1}}</h3><b>In controller: simple_examples.py</b>
  159. {{=CODE("""
  160. def ajaxwiki():
  161. form=FORM(TEXTAREA(_id='text',_name='text'),
  162. INPUT(_type='button',_value='markmin',
  163. _onclick="ajax('ajaxwiki_onclick',['text'],'html')"))
  164. return dict(form=form,html=DIV(_id='html'))
  165. def ajaxwiki_onclick():
  166. return MARKMIN(request.vars.text).xml()
  167. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  168. <p>The markmin wiki markup is described <a href="{{=URL('static','markmin.html')}}">here</a>.
  169. web2py also includes gluon.contrib.<a href="http://code.google.com/p/python-markdown2/">markdown</a>.WIKI helper (markdown2) which converts WIKI markup to HTML following <a href="http://en.wikipedia.org/wiki/Markdown">this syntax</a>. In this example we added a fancy ajax effect.<br/>Try it here: <a class="btn" href="/{{=request.application}}/simple_examples/ajaxwiki">ajaxwiki</a></p>
  170. <h2 id="session_examples">Session Examples</h2>
  171. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: session_examples.py </b>
  172. {{=CODE("""
  173. def counter():
  174. session.counter = (sesstion.counter or 0) + 1
  175. return dict(counter=session.counter)
  176. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: session_examples/counter.html</b>
  177. {{=CODE(open(os.path.join(request.folder,'views/session_examples/counter.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  178. <p>Click to count. The session.counter is persistent for this user and application. Every applicaiton within the system has its own separate session management.
  179. <br/>Try it here: <a class="btn" href="/{{=request.application}}/session_examples/counter">counter</a></p>
  180. <h2 id="template_examples">Template Examples</h2>
  181. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: template_examples.py</b>
  182. {{=CODE("""
  183. def variables():
  184. return dict(a=10, b=20)
  185. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: template_examples/variables.html</b>
  186. {{=CODE(open(os.path.join(request.folder,'views/template_examples/variables.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  187. <p>A view (also known as template) is just an HTML file with &#123;&#123;...&#125;&#125; tags. You can put ANY python code into the tags, no need to indent but you must use pass to close blocks. The view is transformed into a python code and then executed. &#123;&#123;=a&#125;&#125; prints a.xml() or escape(str(a)).
  188. <br/>Try it here: <a class="btn" href="/{{=request.application}}/template_examples/variables">variables</a></p>
  189. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: template_examples.py </b>
  190. {{=CODE("""
  191. def test_for():
  192. return dict()
  193. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: template_examples/test_for.html</b>
  194. {{=CODE(open(os.path.join(request.folder,'views/template_examples/test_for.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  195. <p>You can do for and while loops.
  196. <br/>Try it here: <a class="btn" href="/{{=request.application}}/template_examples/test_for">test_for</a></p>
  197. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: template_examples.py </b>
  198. {{=CODE("""
  199. def test_if():
  200. return dict()
  201. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: template_examples/test_if.html</b>
  202. {{=CODE(open(os.path.join(request.folder,'views/template_examples/test_if.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  203. <p>You can do if, elif, else.
  204. <br/>Try it here: <a class="btn" href="/{{=request.application}}/template_examples/test_if">test_if</a></p>
  205. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: template_examples.py </b>
  206. {{=CODE("""
  207. def test_try():
  208. return dict()
  209. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: template_examples/test_try.html</b>
  210. {{=CODE(open(os.path.join(request.folder,'views/template_examples/test_try.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  211. <p>You can do try, except, finally.
  212. <br/>Try it here: <a class="btn" href="/{{=request.application}}/template_examples/test_try">test_try</a></p>
  213. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: template_examples.py </b>
  214. {{=CODE("""
  215. def test_def():
  216. return dict()
  217. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: template_examples/test_def.html</b>
  218. {{=CODE(open(os.path.join(request.folder,'views/template_examples/test_def.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  219. <p>You can write functions in HTML too.
  220. <br/>Try it here: <a class="btn" href="/{{=request.application}}/template_examples/test_def">test_def</a></p>
  221. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: template_examples.py </b>
  222. {{=CODE("""
  223. def escape():
  224. return dict(message='<h1>text is escaped</h1>')
  225. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: template_examples/escape.html</b>
  226. {{=CODE(open(os.path.join(request.folder,'views/template_examples/escape.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  227. <p>The argument of &#123;&#123;=...&#125;&#125; is always escaped unless it is an object with a .xml() method such as link, A(...), a FORM(...), a XML(...) block, etc.
  228. <br/>Try it here: <a class="btn" href="/{{=request.application}}/template_examples/escape">escape</a></p>
  229. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: template_examples.py </b>
  230. {{=CODE("""
  231. def xml():
  232. return dict(message=XML('<h1>text is not escaped</h1>'))
  233. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: template_examples/xml.html</b>
  234. {{=CODE(open(os.path.join(request.folder,'views/template_examples/xml.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  235. <p>If you do not want to escape the argument of &#123;&#123;=...&#125;&#125; mark it as XML.
  236. <br/>Try it here: <a class="btn" href="/{{=request.application}}/template_examples/xml">xml</a></p>
  237. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: template_examples.py </b>
  238. {{=CODE("""
  239. def beautify():
  240. dict(message=BEAUTIFY(dict(a=1,b=[2,3,dict(hello='world')])))
  241. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: template_examples/beautify.html</b>
  242. {{=CODE(open(os.path.join(request.folder,'views/template_examples/beautify.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  243. <p>You can use BEAUTIFY to turn lists and dictionaries into organized HTML.
  244. <br/>Try it here: <a class="btn" href="/{{=request.application}}/template_examples/beautify">beautify</a></p>
  245. <h2 id="layout_examples">Layout Examples</h2>
  246. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: layout_examples.py </b>
  247. {{=CODE("""
  248. def civilized():
  249. response.menu=[['civilized',True,URL('civilized')],
  250. ['slick',False,URL('slick')],
  251. ['basic',False,URL('basic')]]
  252. response.flash='you clicked on civilized'
  253. return dict(message="you clicked on civilized")
  254. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: layout_examples/civilized.html</b>
  255. {{=CODE(open(os.path.join(request.folder,'views/layout_examples/civilized.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  256. <p>You can specify the layout file at the top of your view. civilized Layout file is a view that somewhere in the body contains &#123;&#123;include&#125;&#125;.
  257. <br/>Try it here: <a class="btn" href="/{{=request.application}}/layout_examples/civilized">civilized</a></p>
  258. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: layout_examples.py </b>
  259. {{=CODE("""
  260. def slick():
  261. response.menu = [['civilized',False,URL('civilized')],
  262. ['slick',True,URL('slick')],
  263. ['basic',False,URL('basic')]]
  264. response.flash = 'you clicked on slick'
  265. return dict(message="you clicked on slick")
  266. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: layout_examples/slick.html</b>
  267. {{=CODE(open(os.path.join(request.folder,'views/layout_examples/slick.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  268. <p>Same here, but using a different template.<br/>Try it here: <a class="btn" href="/{{=request.application}}/layout_examples/slick">slick</a></p>
  269. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: layout_examples.py </b>
  270. {{=CODE("""
  271. def basic():
  272. response.menu=[['civilized',False,URL('civilized')],
  273. ['slick',False,URL('slick')],
  274. ['basic',True,URL('basic')]]
  275. response.flash='you clicked on basic'
  276. return dict(message="you clicked on basic")
  277. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: layout_examples/basic.html</b>
  278. {{=CODE(open(os.path.join(request.folder,'views/layout_examples/basic.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  279. <p>'layout.html' is the default template, every application has a copy of it.
  280. <br/>Try it here: <a class="btn" href="/{{=request.application}}/layout_examples/basic">basic</a></p>
  281. <h2 id="form_examples">Form Examples</h2>
  282. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: form_examples.py</b>
  283. {{=CODE("""
  284. def form():
  285. form=FORM(TABLE(TR("Your name:",INPUT(_type="text",_name="name",requires=IS_NOT_EMPTY())),
  286. TR("Your email:",INPUT(_type="text",_name="email",requires=IS_EMAIL())),
  287. TR("Admin",INPUT(_type="checkbox",_name="admin")),
  288. TR("Sure?",SELECT('yes','no',_name="sure",requires=IS_IN_SET(['yes','no']))),
  289. TR("Profile",TEXTAREA(_name="profile",value="write something here")),
  290. TR("",INPUT(_type="submit",_value="SUBMIT"))))
  291. if form.accepts(request,session):
  292. response.flash="form accepted"
  293. elif form.errors:
  294. response.flash="form is invalid"
  295. else:
  296. response.flash="please fill the form"
  297. return dict(form=form,vars=form.vars)
  298. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  299. <p>You can use HTML helpers like FORM, INPUT, TEXTAREA, OPTION, SELECT to build forms. The "value=" attribute sets the initial value of the field (works for TEXTAREA and OPTION/SELECT too) and the requires attribute sets the validators.
  300. FORM.accepts(..) tries to validate the form and, on success, stores vars into form.vars. On failure the error messages are stored into form.errors and shown in the form.
  301. <br/>Try it here: <a class="btn" href="/{{=request.application}}/form_examples/form">form</a></p>
  302. <h2 id="database_examples">Database Examples</h2>
  303. <p>You can find more examples of the web2py Database Abstraction Layer <a href="http://www.web2py.com/book/default/chapter/06">here</a></p>
  304. <p>Let's create a simple model with users, products (sold by users) and purchases (the database of an animal store). Each user can sell many products (ONE TO MANY). A user can buy many products and each product can have many buyers (MANY TO MANY).</p>
  305. <h3>Example {{=c}}{{c+=1}}</h3><b>in model: db.py</b>
  306. {{=CODE("""
  307. db.define_table(
  308. 'person',
  309. Field('name'),
  310. Field('email'),
  311. format = '%(name)s')
  312. # ONE (person) TO MANY (products)
  313. db.define_table(
  314. 'product',
  315. Field('seller_id',db.person),
  316. Field('name'),
  317. Field('description', 'text'),
  318. Field('picture', 'upload', default=''),
  319. format = '%(name)s')
  320. # MANY (persons) TO MANY (purchases)
  321. db.define_table(
  322. 'purchase',
  323. Field('buyer_id', db.person),
  324. Field('product_id', db.product),
  325. Field('quantity', 'integer'),
  326. format = '%(quantity)s %(product_id)s -> %(buyer_id)s')
  327. purchased = (db.person.id==db.purchase.buyer_id)&(db.product.id==db.purchase.product_id)
  328. db.person.name.requires = IS_NOT_EMPTY()
  329. db.person.email.requires = [IS_EMAIL(), IS_NOT_IN_DB(db, 'person.email')]
  330. db.product.name.requires = IS_NOT_EMPTY()
  331. db.purchase.quantity.requires = IS_INT_IN_RANGE(0, 10)
  332. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  333. <p>
  334. Tables are created if they do not exist (try... except).
  335. Here "purchased" is an Query object, "db(purchased)" would be a Set objects. A Set object can be selected, updated, deleted. Sets can also be intersected. Allowed field types are string, integer, password, text, blob, upload, date, time, datetime, references(*), and id(*). The id field is there by default and must not be declared. references are for one to many and many to many as in the example above. For strings you should specify a length or you get length=32.<br/><br/>
  336. You can use db.tablename.fieldname.requires= to set restrictions on the field values. These restrictions are automatically converted into widgets when generating forms from the table with SQLFORM(db.tablename).
  337. <br/><br/>
  338. define_tables creates the table and attempts a migration if table has changed or if database name has changed since last time. If you know you already have the table in the database and you do not want to attempt a migration add one last argument to define_table <tt>migrate=False</tt>.</p>
  339. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: database_examples.py </b>
  340. {{=CODE("""
  341. response.menu = [['Register Person', False, URL('register_person')],
  342. ['Register Product', False, URL('register_product')],
  343. ['Buy product', False, URL('buy')]]
  344. def register_person():
  345. # create an insert form from the table
  346. form = SQLFORM(db.person).process()
  347. # if form correct perform the insert
  348. if form.accepted:
  349. response.flash = 'new record inserted'
  350. # and get a list of all persons
  351. records = SQLTABLE(db().select(db.person.ALL),headers='fieldname:capitalize')
  352. return dict(form=form, records=records)
  353. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: database_examples/register_person.html</b>
  354. {{=CODE(open(os.path.join(request.folder,'views/database_examples/register_person.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  355. <p>This is a simple user registration form. SQLFORM takes a table and returns the corresponding entry form with validators, etc. SQLFORM.accepts is similar to FORM.accepts but, if form is validated, the corresponding insert is also performed. SQLFORM can also do update and edit if a record is passed as its second argument.
  356. SQLTABLE instead turns a set of records (result of a select) into an HTML table with links as specified by its optional parameters.
  357. The response.menu on top is just a variable used by the layout to make the navigation menu for all functions in this controller.<br/>
  358. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: database_examples.py </b>
  359. {{=CODE("""
  360. def register_product():
  361. form = SQLFORM(db.product).process()
  362. if form.accepted:
  363. response.flash = 'new record inserted'
  364. records = SQLTABLE(db().select(db.product.ALL),
  365. upload = URL('download'), # allows pics preview
  366. headers='fieldname:capitalize')
  367. return dict(form=form, records=records)
  368. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: database_examples/register_product.html</b>
  369. {{=CODE(open(os.path.join(request.folder,'views/database_examples/register_product.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  370. <p>Nothing new here.
  371. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: database_examples.py </b>
  372. {{=CODE("""
  373. def buy():
  374. form = SQLFORM.factory(
  375. Field('buyer_id',requires=IS_IN_DB(db,db.person.id,'%(name)s')),
  376. Field('product_id',requires=IS_IN_DB(db,db.product.id,'%(name)s')),
  377. Field('quantity','integer',requires=IS_INT_IN_RANGE(1,100))).process()
  378. if form.accepted:
  379. # get previous purchese for same product
  380. purchase = db((db.purchase.buyer_id == form.vars.buyer_id)&
  381. (db.purchase.product_id==form.vars.product_id)).select().first()
  382. if purchase:
  383. # if list contains a record, update that record
  384. purchase.update_record(
  385. quantity = purchase.quantity+form.vars.quantity)
  386. else:
  387. # self insert a new record in table
  388. db.purchase.insert(buyer_id=form.vars.buyer_id,
  389. product_id=form.vars.product_id,
  390. quantity=form.vars.quantity)
  391. response.flash = 'product purchased!'
  392. elif form.errors:
  393. response.flash = 'invalid values in form!'
  394. # now get a list of all purchases
  395. records = SQLTABLE(db(purchased).select(),headers='fieldname:capitalize')
  396. return dict(form=form, records=records)
  397. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}<b>and view: database_examples/buy.html</b>
  398. {{=CODE(open(os.path.join(request.folder,'views/database_examples/buy.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  399. <p>Here is a rather sophisticated buy form. It checks that the buyer and the product are in the database and updates the corresponding record or inserts a new purchase. It also does a JOIN to list all purchases.
  400. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: database_examples.py</b>
  401. {{=CODE("""
  402. def download():
  403. return response.download(request,db)
  404. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  405. <p>This controller allows users to download the uploaded pictures of products.
  406. Remember the upload=URL('download') statement in the register_product function. Notice that in the URL path /application/controller/function/a/b/etc a, b, etc are passed to the controller as request.args[0], request.args[1], etc. Since the URL is validated request.args[] always contain valid filenames and no '~' or '..' etc. This is useful to allow visitors to link uploaded files.</p>
  407. <h3>Example {{=c}}{{c+=1}}</h3><b>Using a Smartgrid</b>
  408. <p>All of the above database examples can be condensed in one simple command using the SQLFORM.smartgrid:</p>
  409. {{=CODE("""
  410. def manage_transactions():
  411. grid = SQLFORM.smartgrid(db.person,linked_tables=['product','purchase'],
  412. user_signature=False)
  413. return dict(grid=grid)
  414. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  415. The SQLFORM.smartgrid allows to create/read/delete persons as well as records in the linked tables (product and purchase). It also allows searching with pagination. It can be highly customized. The user_signature=False disables grid access control features which are beyond this simple example.
  416. <h2 id="cache_examples">Cache Examples</h2>
  417. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: cache_examples.py </b>
  418. {{=CODE("""
  419. def cache_in_ram():
  420. import time
  421. t=cache.ram('time',lambda:time.ctime(),time_expire=5)
  422. return dict(time=t,link=A('click to reload',_href=URL(r=request)))
  423. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  424. <p>The output of <tt>lambda:time.ctime()</tt> is cached in ram for 5 seconds. The string 'time' is used as cache key.
  425. <br/>Try it here: <a class="btn" href="/{{=request.application}}/cache_examples/cache_in_ram">cache_in_ram</a></p>
  426. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: cache_examples.py </b>
  427. {{=CODE("""
  428. def cache_on_disk():
  429. import time
  430. t=cache.disk('time',lambda:time.ctime(),time_expire=5)
  431. return dict(time=t,link=A('click to reload',_href=URL(r=request)))
  432. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  433. <p>The output of <tt>lambda:time.ctime()</tt> is cached on disk (using the shelve module) for 5 seconds.
  434. <br/>Try it here: <a class="btn" href="/{{=request.application}}/cache_examples/cache_on_disk">cache_on_disk</a></p>
  435. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: cache_examples.py </b>
  436. {{=CODE("""
  437. def cache_in_ram_and_disk():
  438. import time
  439. t=cache.ram('time',lambda:cache.disk('time',
  440. lambda:time.ctime(),time_expire=5),time_expire=5)
  441. return dict(time=t,link=A('click to reload',_href=URL(r=request)))
  442. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  443. <p>The output of <tt>lambda:time.ctime()</tt> is cached on disk (using the shelve module) and then in ram for 5 seconds. web2py looks in ram first and if not there it looks on disk. If it is not on disk it calls the function. This is useful in a multiprocess type of environment. The two times do not have to be the same.
  444. <br/>Try it here: <a class="btn" href="/{{=request.application}}/cache_examples/cache_in_ram_and_disk">cache_in_ram_and_disk</a></p>
  445. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: cache_examples.py </b>
  446. {{=CODE("""
  447. @cache(request.env.path_info,time_expire=5,cache_model=cache.ram)
  448. def cache_controller_in_ram():
  449. import time
  450. t=time.ctime()
  451. return dict(time=t,link=A('click to reload',_href=URL(r=request)))""".strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  452. <p>Here the entire controller (dictionary) is cached in ram for 5 seconds. The result of a select cannot be cached unless it is first serialized into a table <tt>lambda:SQLTABLE(db().select(db.user.ALL)).xml()</tt>. You can read below for an even better way to do it.
  453. <br/>Try it here: <a class="btn" href="/{{=request.application}}/cache_examples/cache_controller_in_ram">cache_controller_in_ram</a></p>
  454. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: cache_examples.py </b>
  455. {{=CODE("""
  456. @cache(request.env.path_info,time_expire=5,cache_model=cache.disk)
  457. def cache_controller_on_disk():
  458. import time
  459. t=time.ctime()
  460. return dict(time=t,link=A('click to reload',_href=URL(r=request)))
  461. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  462. <p>Here the entire controller (dictionary) is cached on disk for 5 seconds. This will not work if the dictionary contains unpickleable objects.
  463. <br/>Try it here: <a class="btn" href="/{{=request.application}}/cache_examples/cache_controller_on_disk">cache_controller_on_disk</a></p>
  464. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: cache_examples.py </b>
  465. {{=CODE("""
  466. @cache(request.env.path_info,time_expire=5,cache_model=cache.ram)
  467. def cache_controller_and_view():
  468. import time
  469. t=time.ctime()
  470. d=dict(time=t,link=A('click to reload',_href=URL(r=request)))
  471. return response.render(d)
  472. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  473. <p><tt>response.render(d)</tt> renders the dictionary inside the controller, so everything is cached now for 5 seconds. This is best and fastest way of caching!
  474. <br/>Try it here: <a class="btn" href="/{{=request.application}}/cache_examples/cache_controller_and_view">cache_controller_and_view</a></p>
  475. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: cache_examples.py </b>
  476. {{=CODE("""
  477. def cache_db_select():
  478. import time
  479. db.person.insert(name='somebody',email='gluon@mdp.cti.depaul.edu')
  480. records = db().select(db.person.ALL,cache=(cache.ram,5))
  481. if len(records)>20: db(db.person.id>0).delete()
  482. return dict(records=records)
  483. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  484. <p>The results of a select are complex unpickleable objects that cannot be cached using the previous method, but the select command takes an argument <tt>cache=(cache_model,time_expire)</tt> and will cache the result of the query accordingly. Notice that the key is not necessary since key is generated based on the database name and the select string.
  485. <h2 id="ajax_examples">Ajax Examples</h2>
  486. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: ajax_examples.py</b>
  487. {{=CODE("""
  488. def index():
  489. return dict()
  490. def data():
  491. if not session.m or len(session.m)==10: session.m=[]
  492. if request.vars.q: session.m.append(request.vars.q)
  493. session.m.sort()
  494. return TABLE(*[TR(v) for v in session.m]).xml()
  495. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  496. <b>In view: ajax_examples/index.html</b>
  497. {{=CODE(open(os.path.join(request.folder,'views/ajax_examples/index.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  498. <p>The javascript function "ajax" is provided in "web2py_ajax.html" and included by "layout.html". It takes three arguments, a url, a list of ids and a target id. When called, it sends to the url (via a get) the values of the ids and display the response in the value (of innerHTML) of the target id.
  499. <br/>Try it here: <a class="btn" href="/{{=request.application}}/ajax_examples/index">index</a></p>
  500. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: ajax_examples.py </b>
  501. {{=CODE("""
  502. def flash():
  503. response.flash='this text should appear!'
  504. return dict()
  505. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  506. <p>Try it here: <a class="btn" href="/{{=request.application}}/ajax_examples/flash">flash</a></p>
  507. <h3>Example {{=c}}{{c+=1}}</h3><b>In controller: ajax_examples.py </b>
  508. {{=CODE("""
  509. def fade():
  510. return dict()
  511. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  512. <b>In view: ajax_examples/fade.html </b><br/>
  513. {{=CODE(open(os.path.join(request.folder,'views/ajax_examples/fade.html'),'r').read(),language='html',link=URL('global','vars'),_class='boxCode')}}
  514. <p>Try it here: <a class="btn" href="/{{=request.application}}/ajax_examples/fade">fade</a></p>
  515. <h3>Excel-like spreadsheet via Ajax</h3>
  516. Web2py includes a widget that acts like an Excel-like spreadsheet and can be used to build forms
  517. <a class="btn btn-small" href="{{=URL('spreadsheet','index')}}">read more</a>.
  518. <h2 id="testing_examples">Testing Examples</h2>
  519. <h3>Example {{=c}}{{c+=1}}</h3>
  520. <p>Using the Python doctest notation it is possible to write tests for all controller functions. Tests are then run via the administrative interface which generates a report. Here is an example of a test in the code:
  521. {{=CODE("""
  522. def index():
  523. '''
  524. This is a docstring. The following 3 lines are a doctest:
  525. >>> request.vars.name='Max'
  526. >>> index()
  527. {'name': 'Max'}
  528. '''
  529. return dict(name=request.vars.name)
  530. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  531. <p></p>
  532. <h2 id="streaming_examples">Streaming Examples</h2>
  533. <h3 id="example_stream">Example {{=c}}{{c+=1}}</h3>
  534. <p>It is very easy in web2py to stream large files. Here is an example of a controller that does so:</p>
  535. {{=CODE("""
  536. def streamer():
  537. import os
  538. path=os.path.join(request.folder,'private','largefile.mpeg4')
  539. return response.stream(open(path,'rb'),chunk_size=4096)
  540. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  541. <p>By default all static files and files stored in 'upload' fields in the database are streamed when larger than 1MByte.</p>
  542. </p>web2py automatically and transparently handles PARTIAL_CONTENT and RANGE requests.</p>
  543. <h2 id="xmlrpc_examples">XML-RPC Examples</h2>
  544. <h3>Example {{=c}}{{c+=1}}</h3>
  545. <p>Web2py has native support for the XMLRPC protocol. Below is a controller function "handler" that exposes two functions, "add" and "sub" via XMLRPC. The controller "tester" executes the two function remotely via xmlrpc.</p>
  546. {{=CODE("""
  547. from gluon.tools import Service
  548. service = Service(globals())
  549. @service.xmlrpc
  550. def add(a,b): return a+b
  551. @service.xmlrpc
  552. def sub(a,b): return a-b
  553. def call(): return service()
  554. def tester():
  555. import xmlrpclib
  556. server=xmlrpclib.ServerProxy('http://hostname:port/app/controller/call/xmlrpc')
  557. return str(server.add(3,4)+server.sub(3,4))
  558. """.strip(),language='web2py',link=URL('global','vars'),_class='boxCode')}}
  559. </div>
  560. </div>
  561. {{block sidebar}}{{end}}