1. Start web2py
2. Usually blog will have post and view the post details
3. Validations required to validate the post
4. Database design to store title, content, image category , blog posted date etc..
First we need to create model
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg39_RmCa2pWI64GV60REG8TH02dtr8bOXllQp-L_U9SQZi1QmHKo2yfpOyoMWNKRKv4pCQwhLmy50BNSwvbbqHeJew0xugYkfbXA__iUraSV8tAezvHy6Bo5c82gNSQIq_-oP04Q6fBMU/s320-rw/img_1.png)
5. Edit the db.py file
6.End of the file add the below code, to create a table 'blog' along with columns
db.define_table('blog', Field('blog_title'), Field('blog_details'), Filed('blog_image'), Field('blog_url'), Field('blog_category'), Field('blog_date_posted'))
We need to use validators inside model, more validators look inside
http://web2py.com/examples/static/web2py_cheatsheet.pdf
db.define_table('blog', Field('blog_title', requires=IS_NOT_EMPTY()), Field('blog_details', type='text'), Filed('blog_image',requires=IS_URL()), Field('blog_url',requires=IS_URL()), Field('blog_category',requires=IS_IN_SET(['News','Events','Technology','Business'])), Field('blog_date_posted', type='date',requires=IS_DATE()))
7. Need to add validation for table details
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEih3C5uSoEUN-m9zNLrObHY74lfdDioCSh5mzDdCWIoiTFltclL8RnXt1mEFv7SKATkmNltCO25nz12MGr4Z7iuT6_2RE8RCw196RzXaLJMnsqM5U3eIHYTZ6EpQYaKHh18iKaqPbXS7NM/s320-rw/img_2.png)
8.Now we need to create a controller, to post the blog app
9.Create controller name called blog under controller section
10.Edit blog.py
11. Add two methods one is called post and one is view
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhmd6cVfErxkeCtQhGaiBL1bEzHiNfVQ_USIouILnCIozICYRCj5PONz5oV6-0AvssSy_eHdrOvRoYz36lSzn36DQ3j7bnAHS4-SoG07_Ftblda8299mJr-sZgvYdqjZ6G9Rc33n-rjTGc/s320-rw/img_3.png)
def post(): form =SQLFORM(db.blog).process() return locals() def view(): rows = db(db.blog).select(orderby=~db.blog.id) return locals()
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgp3w9JNEXqPv7Cd7oxBJf1HZ24YmptTWjMw11mP38OS9BQKr4UwZT3wxmwkMIlexpVyXNKy7i2vbOKlmILDn_iZdc7nuGg3TjfZMdiVBm_4HXVCd2WrGwxXe_zhyX_q48xFA56LntZ6qM/s320-rw/img_4.png)
12.Now we need to create views as blog/post and blog/view
13.Edit the blog/post.py
{{extend 'layout.html'}}post a blog as you like
{{=form}}
http://127.0.0.1:8000/sivaweb2py/blog/post
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLkt3LE0D4ypN3SFGq3lxorhuAJ5OAoTjKrDjxAVlahyHifqqOpCKMVxC0IJ8JNeLBd1wxIekqc8Iy9i7wRDyNTMpYtfSMHGBSPX5D7-T6ljxYkm-0oP-YinK1HOzYzHL0v7z7XIv1SAo/s320-rw/img_5.png)
14.If you submit without entering any values, then it will show the validation errors
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK-u2ccvo9VlPggmf-4dV-RRX9nf7BNIG0dgFZiRSkFlPLnARGzp2YreTb7pmiE51pzkduWzyUSUxyfQoMZPKYpZl9ib5qolqEayVccX21rg0KJA66CMgIWUpqvahvf1WuZfvFPEVOTwU/s320-rw/img_6.png)
Provide the details as required and submit the same. You can see the success message.
Need to know more about SQLFORMS and validators
http://web2py.com/books/default/chapter/29/07/forms-and-validators
Now we need to update the blog
Edit the blog.py and add the below code for update method
def update(): record = db.blog(request.args(0)) or redirect (URL(post)) form = SQLFORM(db.blog,record) if form.process().accepted: response.flash = T('Record Updated') else: response.flash=T('Please complete the form') return locals()
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgOzFAoSwO7iaKFrpm6NBcdisfVkY4vmpUGzBunvlDBO05nOs0EsgMTs4GateYB4dx6ZyNFgR8e5ffpT5SEV45rNCGZNZIjE4PXU0OsbxkKrHy2p2x1Lndd6dIMU3DeC49ysDVXJW2oHEQ/s320-rw/img_7.png)
Create update view under views section
{{extend 'layout.html'}}Update the form
{{=form}}
Click on the below link to update the record or provide number after update method which ever record want to update
http://127.0.0.1:8000/sivaweb2py/blog/update/1
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhScld2d0x30u0TtijAmF85psJOjygjtTm_eICSJosN6HK_ASU_Q_72dF6MjFhtYdLj8HEF9oPK-aZEG6voYEHGKX-a9Uy1WSlyaccdUht4r4IX2wfhJyY-eBRFpA10jkwdzWW8RtNeqMA/s320-rw/img_8.png)
Now we need to create the view page
{{extend 'layout.html'}}View the blog
{{ for x in rows:}}{{ pass }}![]()
categrory:{{=x.blog_category}}
blog Details:{{=x.blog_details}}
blog posted:{{=x.blog_date_posted}}
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhOO33k-NtiJ4S7smCUdI6PO9YURWkBNSAxzaHzw2dOrBFHv4S4KjIQnYpbgCBBomCW-Qum5ADARPSSZFKGa_aBSgKNWMdMLU8vq_PSoA1ldtmrdD15ba8rARTLBpfxvlL1TwCdG-ZhtlA/s320-rw/img_10.png)
After adding the above code in view.py, if we check the result using below url
http://127.0.0.1:8000/sivaweb2py/blog/view
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhr4meZNGwvAFrbmFsvO7UrSWqB94iMoH50KjOVPgyFiW2kLIZFpfkWbMCC0YU7njlogonwotAvCCd5NzXwR_XZ-NQmAVAlhykE5laq-D1tWTtiuXPEzbikb0kg_wxEOU0RonCqFG6U6zU/s320-rw/img_9.png)
This is how we can create a blog app using web2py.