Blog app with flask

Blog app with options for posting,viewing post,and commenting option enabled on each post can be efficiently developed using flask.


So what is flask?
The following definition from http://flask.pocoo.org/ explains it perfectly.

"Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions."

Here i used postgresql for the database part. psycopg2 module is widely used in python for the postgresql database handling.I used the same in my app.In the app a sign_in part is provided so that posting is allowed only to the admin. Username for my app is 'admin' and password is'1234'.

If we grab a basic knowledge in flask then it will be easy to grasp the code i wrote for this app.The main modules i used are Flask,render_template,request etc.

For a quick reference i will explain the post_store function in my app.

The HTML part is as given below

 <h4>Post your thoughts here</h4>  
 <form method="post" action="{{ url_for('post_store')}}">  
 <div align="center">  
 <p> Title </p>  
 <input type="text" name="name" style="width: 250px"><br>  
 <textarea name="blogpost" cols="45" rows="15">  
 </textarea><br>  
 <input type="submit" value="Submit" />  
 </div>  
 </form>  

so here you can see form's action part given as '{{ url_for('post_store')}}'.Which means after submitting the form the action is to connect to the post store function in python.I hope you are already aware on "form" tag in html.

Given below is the post_store function written in python.

  
 @app.route('/post',methods=['POST'])  
 def post_store():  
      conn=psycopg2.connect(database='nidhin')  
      c=conn.cursor()  
      c.execute("INSERT INTO blogspot (author,post,day,time) VALUES (%s,%s,%s,%s)",[request.form['name'],request.form['blogpost'],strftime("%d %b %Y ", gmtime()),strftime("%H:%M:%S ", gmtime())])  
      conn.commit()  
      conn.close()  
      return render_template('post.html')  

The first line @app.route('/post',methods=['POST']) indicates that the following function will work if the url is 'some_main_url'/post and the method is POST.
app is connected to the postgresql using psycopg2 and the data is from the form is stored in it.

If you are a beginner and cannot make a word out of the things above, then you should try this Flask mega tutorial by miguelgrinberg.It helped me a lot.

So in this way i tried to test flask by creating a blog app and honestly i am very much satisfied with the outcome.I felt the differnce after getting tired by using conventional commands in python.django is prior to flask i know.But i never tried it.So from my experience flask made my day.If you are reading this you should give it a try.

And i stored my app in heroku.So you can test it by clicking this link.
For the funky version click here.


To see the code click the links below.There are three versions.

version one is blog app built on virtual environment.
version two is blog app built normally.
version three is the blog app with funky theme.






No comments:

Post a Comment