AlterEgo
old web2py blog
Useful Links
List all entries
Book
Interactive Examples
F.A.Q.
Free Applications
Plugins
Recipes
Demo of Admin Interface
Semantic web extension
Some of the information here may be outdated, please check the book instead
Edit page
Title:
Security Code:
Body:
(use
this
wiki markup)
Just for fun. Inspired by [this](http://articles.sitepoint.com/article/django-crash-course) crash course in Django. It is a little different because we decided to: - move some of the business logic from the view of the Django example into the controller and to define the comment table explicitly instead of using a plugin. - enable markdown WIKI syntax in both blog posts and comments - require login for commenting #Download and install There is no installation process. Download web2py from [http://web2py.com](http://web2py.com), unzip and click on "web2py". It will ask you to choose a one time administrator password. Choose one. #Create a new app Visit the admin interface at http://127.0.0.1:8000/admin Login with the password you choose above. In the textbox on the right type the name for the new app, for example "blog", and press "submit". You will be redirected to a design page that let you edit the new page. Click on "database administration" to get a web based interface to your database. #Create the model In the design page create a new model file called "db_blog.py" by typing the name in appropriate textbox. In the file write: db.define_table('post', Field('title',length=256), Field('body','text',requires=IS_NOT_EMPTY()), Field('author',db.auth_user)) db.define_table('comment', Field('post',db.post,writable=False,readable=False), Field('author',db.auth_user,writable=False,readable=False), Field('body','text',requires=IS_NOT_EMPTY())) Go to the database administrative interface to add some posts: http://127.0.0.1:8000/blog/appadmin IMPORTANT!!! Make sure you register and login with the application else posts will have no author and they cannot be displayed. #Edit the controller In the design page edit the controller "default.py" by clicking on the appropriate link, edit the "index" function and create a "view_post" function as show below: def index(): return dict(posts=db().select(db.post.ALL)) def view_post(): post = db.post[request.args(0)] or redirect(URL(r=request,f='index')) if auth.is_logged_in(): db.comment.post.default = post.id db.comment.author.default = auth.user.id form = crud.create(db.comment) else: form = A("login to comment",_href=URL(r=request,f='user/login')) comments = db(db.comment.post==post.id).select(db.comment.ALL) return dict(post=post, form=form, comments=comments) At this point the app is fully working although you may not like the default views so... #Adding Views Edit the view "default/index.html" associated to the "index()" action. Replace the content of the view with: {{extend 'layout.html'}} {{from gluon.contrib.markdown import WIKI}} <h1>Posts</h1> {{ for post in posts:}} <h2> <a href="{{=URL(r=request,f='view_post',args=post.id)}}"> {{=post.title}} </a> </h2> {{=WIKI(post.body)}} {{ pass }} Create now a view for a post and its comments. Do this by creating a new view file "default/view_post.html" {{extend 'layout.html'}} {{from gluon.contrib.markdown import WIKI}} <h1>{{=post.title}}</h1> <h2>by {{=post.author.first_name}}</h2> {{=WIKI(post.body)}} <h2>Comments</h2> {{for comment in comments:}} <blockquote> {{=WIKI(comment.body)}} <em>by {{=comment.author.first_name}}</em> </blockquote> {{ pass }} {{=form}} #The End You can now visit your blog at http://127.0.0.1:8000/blog and view the individual blog pages at http://127.0.0.1:8000/blog/default/view_post/1 http://127.0.0.1:8000/blog/default/view_post/2 http://127.0.0.1:8000/blog/default/view_post/3 ... Commenting requires login. You can login, register, manage your profile, etc here: http://127.0.0.1:8000/blog/default/user/login http://127.0.0.1:8000/blog/default/user/register http://127.0.0.1:8000/blog/default/user/logout http://127.0.0.1:8000/blog/default/user/retrieve_password http://127.0.0.1:8000/blog/default/user/profile http://127.0.0.1:8000/blog/default/user/change_password You can administer you blog (manage accounts, create and delete posts and comments) at http://127.0.0.1:8000/blog/appadmin You can edit your source code online via http://127.0.0.1:8000/admin If you do not like the default URLs you can change the mapping by editing routes.py You can also do reverse url mapping so that you do not need to edit code or views when url changes. Links will not break. #Google App Engine This code works on GAE as it is. It does not need any tweaking. You need to run the source version of web2py, not the binary distribution. You must edit the provided "app.yaml" in the main web2py folder and replace "web2py" with the name of your GAE application-id. Then deploy it on GAE cd /path/to/where/web2py/is appcfg.py update web2py You can now find your app running at http://gae-application-id.appspot.com
© 2008-2010 by Massimo Di Pierro - All rights reserved - Powered by
web2py
- design derived from a theme by
the earlybird
The content of this book is released under the
Artistic License 2.0
- Modified content cannot be reproduced.