As in the previous example,there used sqlite3.Here database is postgresql
Blog app act as a small scale blogspot account which provides options such as posting commenting etc.Blog app uses postgresql database to store the post and comments. psycopg2 module provided by python is used for it.Posting can be done only by the admin. Admin has to log in to post to avoid fake users.
documentation on flask
These kind of things takes a lot of work to code in normal networking in python. Flask makes everything easier.Also flask helps the debugging much easier. The blog app is my first work in flask and it didn't give me much headache.
Let me show you some basic items in flask.
@app.route('url')
def func_name():
.....
Here first line tells us 'Where to go'.For example @app.route('/') means home page, @app.route('/post') means post link(ie home/post/).
Under that we define the function to tell the app 'how to behave' when these links are provided. We can render an html page, read posts from forum, save data etc.
In short if have a 'not bad' knowledge in flask then web based apps can be easily made. Flask is a great web framework provided by python where rookies can have a shot too.
Postgresql setup:
sudo apt-get install libpq-dev python-dev
sudo apt-get install postgresql
sudo -u postgres createuser --superuser $USER
sudo -u postgres psql
sudo -u postgres createdb $USER
Now you can create database in terminal using command psql
create database databasename;
\c databasename; #to connect database
create table posts(id integer,title TEXT,text INT);
\d #show tables
\d tablename #to show corresponding table
then use select query and more...
Connection:
To connect postgresql,we must install psycopg2
sudo apt-get install python-psycopg2
In a program connection done through this lines.for example:
import psycopg2
con = psycopg2.connect(database='firstdb')
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS paintstore1")
cur.execute("CREATE TABLE paintstore1(id serial,title text,imagedata text)")
con.commit()
con.close()
No comments:
Post a Comment