Changeset 1457
- Timestamp:
- 05/09/06 16:22:46 (3 years ago)
- Files:
-
- branches/1.0/newdocs/docs/tutorials/wiki20/index.html (modified) (2 diffs)
- branches/1.0/newdocs/docs/tutorials/wiki20/page2.html (modified) (4 diffs)
- branches/1.0/newdocs/docs/tutorials/wiki20/page3.html (modified) (3 diffs)
- branches/1.0/newdocs/docs/tutorials/wiki20/page4.html (modified) (3 diffs)
- branches/1.0/newdocs/docs/tutorials/wiki20/page5.html (modified) (2 diffs)
- branches/1.0/newdocs/docs/tutorials/wiki20/page6.html (modified) (2 diffs)
- branches/1.0/setup.cfg (modified) (1 diff)
- branches/1.0/turbogears/docgen.py (modified) (1 diff)
- branches/1.0/turbogears/qstemplates/quickstartbig/+package+/controllers/root.py_tmpl (modified) (1 diff)
- branches/1.0/turbogears/qstemplates/quickstart/+package+/controllers.py_tmpl (modified) (1 diff)
- branches/1.0/turbogears/toolbox/widgets.kid (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.0/newdocs/docs/tutorials/wiki20/index.html
r991 r1457 55 55 <pre class="command">Enter project name: Wiki 20 56 56 Enter package name [wiki20]: wiki20 57 </pre> 57 Do you need Identity (usernames/passwords) in this project? [no] no 58 </pre> 58 59 59 60 <p>This creates a few files in a directory tree just below your current directory. Let's go in there and you can take a look around.</p> … … 88 89 <textarea name="code" class="py" rows="3" cols="60"> 89 90 class Page(SQLObject): 90 pagename =StringCol(alternateID=True, length=30)91 data =StringCol()91 pagename = UnicodeCol(alternateID=True, length=30) 92 data = UnicodeCol() 92 93 </textarea> 93 94 branches/1.0/newdocs/docs/tutorials/wiki20/page2.html
r596 r1457 12 12 <h2>Pointing to a database</h2> 13 13 14 <p>TurboGears has a minimum of required configuration. It <em>does</em> need to know where your database lives. The quickstart creates a couple of simple Pythonconfig files for you.</p>14 <p>TurboGears has a minimum of required configuration. It <em>does</em> need to know where your database lives. The quickstart creates a couple of simple config files for you.</p> 15 15 16 <p>Since we're working in a development environment and not a production deployment one, edit the "devcfg.py" file. You'll just need to uncomment the sqlobject.dburi line that corresponds to your database and provide the proper connection info.</p> 16 <p>If you have sqlite installed, you can get started in development without configuring anything (skip the rest of this section).</p> 17 18 <p>Since we're working in a development environment and not a production deployment one, edit the "dev.cfg" file. You'll just need to uncomment the sqlobject.dburi line that corresponds to your database and provide the proper connection info.</p> 17 19 18 20 <p>Restart the web server by hitting control-C and running the startup script again:</p> … … 41 43 <textarea name="code" class="html" cols="60"><![CDATA[ 42 44 <div style="float:right; width: 10em"> 43 Viewing <span py:replace="page name">Page Name Goes Here</span>45 Viewing <span py:replace="page.pagename">Page Name Goes Here</span> 44 46 <br/> 45 47 You can return to the <a href="/">FrontPage</a>. 46 48 </div> 47 49 48 <div py:replace="XML( data)">Page text goes here.</div>50 <div py:replace="XML(page.data)">Page text goes here.</div> 49 51 ]]> 50 52 </textarea> … … 54 56 <p>TurboGears greatly reduces the amount of code you need to write, but it does not eliminate it. Let's add a couple of imports to the top of controllers.py:</p> 55 57 <textarea name="code" class="py"> 56 from model import Page58 from wiki20.model import Page 57 59 from docutils.core import publish_parts 58 60 </textarea> … … 68 70 69 71 <textarea name="code" class="py"> 70 @ turbogears.expose(html=".templates.page")72 @expose("wiki20.templates.page") 71 73 def index(self, pagename="FrontPage"): 72 74 page = Page.byPagename(pagename) branches/1.0/newdocs/docs/tutorials/wiki20/page3.html
r596 r1457 38 38 39 39 <textarea name="code" cols="60" class="xhtml"><![CDATA[ 40 <form action="save" method="post"> 41 <input type="hidden" name="pagename" py:attrs="value=pagename"/> 42 <textarea name="data" py:content="data" rows="10" cols="60"/> 43 <input type="submit" name="submit" value="Save"/> 44 </form> 45 ]]> 40 $${wikiform.display(page, action="save")} 41 ]]> 46 42 </textarea> 43 44 <p>wikiform is a TurboGears widget. Widgets make data entry forms easy. We need to define that form, just above our Root class in controllers.py. Here's what it looks like:</p> 45 46 <textarea name="code" class="py" cols="60"><![CDATA[ 47 from turbogears.widgets import * 48 49 class wikifields(WidgetsList): 50 pagename = TextField() 51 data = TextArea(rows=10, cols=50) 52 53 wikiform = TableForm(fields=wikifields()) 54 ]]></textarea> 47 55 48 56 <p>We need something to use this template now. We'll add an "edit" method to our controller:</p> 49 57 50 58 <textarea name="code" cols="60" class="py"><![CDATA[ 51 @ turbogears.expose(html=".templates.edit")59 @expose("wiki20.templates.edit") 52 60 def edit(self, pagename): 53 61 page = Page.byPagename(pagename) 54 return dict(page name=page.pagename, data=page.data)62 return dict(page=page, wikiform=wikiform) 55 63 ]]> 56 64 </textarea> … … 68 76 <h2>Saving our edits</h2> 69 77 70 <p> The form we made in the last section had an action of"save". So, we need to make a method called save in our controller. Here's what it looks like:</p>78 <p>When we displayed our wikiform in the last section, the action was "save". So, we need to make a method called save in our controller. Here's what it looks like:</p> 71 79 72 80 <textarea name="code" cols="60" class="py"><![CDATA[ 73 @turbogears.expose() 74 def save(self, pagename, data, submit): 81 @expose() 82 @validate(form=wikiform) 83 def save(self, pagename, data): 75 84 page = Page.byPagename(pagename) 76 85 page.data = data … … 79 88 ]]></textarea> 80 89 81 <p>The <tt>submit</tt> parameter is supplied because it will be passed by the form. If we don't supply this, Python will complain about an unexpected parameter. An alternative is to use **keywords.</p>90 <p>Though we're not using validation here, by telling validate that we're getting input from the wikiform, the submit button that is passed in will automatically be stripped out. Otherwise, the submit button would be passed to the method, just like any other parameter that comes from the web.</p> 82 91 83 92 <p>Interesting things to note about this:</p> branches/1.0/newdocs/docs/tutorials/wiki20/page4.html
r596 r1457 15 15 16 16 <textarea name="code" cols="60" class="py"><![CDATA[ 17 @ turbogears.expose(html=".templates.page")17 @expose() 18 18 def default(self, pagename): 19 19 return self.index(pagename) … … 47 47 page = Page.byPagename(pagename) 48 48 except SQLObjectNotFound: 49 raise turbogears.redirect("/notfound", pagename= pagename)49 raise redirect("/notfound", pagename = pagename) 50 50 ]]></textarea> 51 51 … … 59 59 60 60 <textarea name="code" cols="60" class="py"><![CDATA[ 61 @ turbogears.expose(html=".templates.edit")61 @expose("wiki20.templates.edit") 62 62 def notfound(self, pagename): 63 return dict(pagename=pagename, data="", new=True) 63 page = Page(pagename=pagename, data="") 64 return dict(page=page, wikiform=wikiform) 64 65 ]]></textarea> 65 66 <p>Notice that the dictionary includes a variable called "new" for use in the template. Change the edit method to set new=False when it returns its dictionary:</p> 67 68 <pre class="command"> return dict(pagename=page.pagename, data=page.data, new=False)</pre> 69 70 <p>We need to be able to save a new item, so, we'll have to change the save method like this:</p> 71 72 <textarea name="code" cols="60" class="py"><![CDATA[ 73 @turbogears.expose() 74 def save(self, pagename, data, submit, new): 75 if new == "True": 76 page = Page(pagename=pagename, data=data) 77 else: 78 page = Page.byPagename(pagename) 79 page.data = data 80 turbogears.flash("Changes saved!") 81 raise turbogears.redirect("/%s" % pagename) 82 ]]></textarea> 83 84 <p>With SQLObject, just instantiating an object is enough to insert it in the database.</p> 85 86 <p>The one last thing we need to do is pass along the "new" variable to the save method via the form in edit.kid:</p> 87 88 <pre class="command"><![CDATA[ <input type="hidden" name="new" value="$${new}" />]]></pre> 66 67 <p>With SQLObject, just instantiating an object is enough to insert it in the database, so this method will create a brand new page in our database.</p> 89 68 90 69 <p>Give it a try! You should be able to create new pages now.</p> branches/1.0/newdocs/docs/tutorials/wiki20/page5.html
r596 r1457 10 10 11 11 <body> 12 <h2>Converting incoming arguments</h2>13 14 <p>Something that's a little ugly in that save method is the 'if new == "True"'. Wouldn't it be nicer to just use the more pythonic 'if new'? Piece of cake... we just need to use a validator. First, we need an import:</p>15 16 <pre class="command">from turbogears import validators</pre>17 18 <p>Then, we specify the validator. The StringBoolean validator converts a string like "True" into the proper boolean. Doing that, we can change to using "if new". Here's the top of the save method:</p>19 20 <textarea name="code" cols="60" class="py"><![CDATA[21 @turbogears.expose()22 @turbogears.validate(validators=dict(new=validators.StringBoolean()))23 def save(self, pagename, data, submit, new):24 if new:25 page = Page(pagename=pagename, data=data)26 ]]></textarea>27 28 12 <h2>Adding a page list</h2> 29 13 … … 58 42 59 43 <textarea name="code" cols="60" class="py"><![CDATA[ 60 @ turbogears.expose(html=".templates.pagelist")44 @expose("wiki20.templates.pagelist") 61 45 def pagelist(self): 62 46 pages = [page.pagename for page in Page.select(orderBy=Page.q.pagename)] branches/1.0/newdocs/docs/tutorials/wiki20/page6.html
r961 r1457 12 12 <h2>You want AJAX? We got AJAX!</h2> 13 13 14 <p>This part of the tutorial is not technically AJAX. The "X" in AJAX stands for XML. I'm going to use <a href="http://www.json.org/" target="_blank">JSON</a> instead. JSON is easy and lightweight and efficient to use for all browsers. To use JSON for this TurboGears example, we just have to tell TurboGears that we want to use it. Change the expose decorator for our new pagelist method to be:</p>14 <p>This part of the tutorial is not technically AJAX. The "X" in AJAX stands for XML. I'm going to use <a href="http://www.json.org/" target="_blank">JSON</a> instead. JSON is easy and lightweight and efficient to use for all browsers. To use JSON for this TurboGears example, we just have to tell TurboGears that we want to use it. Just below the expose decorator, let's add another:</p> 15 15 16 <pre class="command">@ turbogears.expose(html=".templates.pagelist", allow_json=True)</pre>16 <pre class="command">@expose("json")</pre> 17 17 18 18 <p>Now, point your browser at <a href="http://localhost:8080/pagelist?tg_format=json" target="_blank">http://localhost:8080/pagelist?tg_format=json</a>. There's your pagelist in JSON format.</p> … … 24 24 <p>The first thing we need to do is have MochiKit included in all of our pages. We can actually do this right from the project configuration file. This file is config/app.cfg in the Python package that quickstart created for your project. Open that up, and in your editor you'll find a commented out "VIEW" part of the config. Uncomment the line that says "tg.mochikit_all" and change the value to True. The line will end up looking like this:</p> 25 25 26 <pre class="command"> "tg.mochikit_all" : True,</pre>26 <pre class="command">tg.mochikit_all = True</pre> 27 27 28 28 <p>This setting will tell TurboGears to include MochiKit in every page. After making this configuration change, <b>restart the server.</b></p> branches/1.0/setup.cfg
r1305 r1457 8 8 9 9 [egg_info] 10 tag_build = dev11 tag_svn_revision = true10 # tag_build = dev 11 # tag_svn_revision = true 12 12 13 13 [aliases] branches/1.0/turbogears/docgen.py
r1098 r1457 225 225 if self.noprintable: 226 226 return 227 self._make_printable(os.path.join("docs", "tutorials", "wiki20"), 3) 227 228 self._make_printable(os.path.join("docs", "wiki20")) 228 self._make_printable(os.path.join("docs", "tutorials", "wiki20"), 3)229 229 230 230 def _make_printable(self, tutdir, up_to_root=2): branches/1.0/turbogears/qstemplates/quickstartbig/+package+/controllers/root.py_tmpl
r1382 r1457 4 4 5 5 import turbogears 6 from turbogears import controllers, expose, redirect6 from turbogears import controllers, expose, validate, redirect 7 7 #if $identity != "none" 8 8 from turbogears import identity branches/1.0/turbogears/qstemplates/quickstart/+package+/controllers.py_tmpl
r1382 r1457 4 4 5 5 import turbogears 6 from turbogears import controllers, expose, redirect6 from turbogears import controllers, expose, validate, redirect 7 7 #if $identity != "none" 8 8 from turbogears import identity branches/1.0/turbogears/toolbox/widgets.kid
r1455 r1457 51 51 <div class="tabbertab"> 52 52 <h2>Source Code</h2> 53 <textarea name="code" class="py" py:content="widgetdesc.source"/>53 <textarea py:content="widgetdesc.source" rows="10" cols="60" wrap="none"/> 54 54 </div> 55 55 <div class="tabbertab" py:if="widgetdesc.for_widget.params"> … … 64 64 <div py:if="widgetdesc.for_widget.template" class="tabbertab"> 65 65 <h2>Template</h2> 66 <textarea name="code" class="xml" py:content="widgetdesc.for_widget.template" />66 <textarea rows="10" cols="60" wrap="none" py:content="widgetdesc.for_widget.template" /> 67 67 </div> 68 68 </div>