A full-fledged blog application build using flask. FlaskBlog is a comprehensive blog application developed using Flask, a micro web framework in Python. This README provides a step-by-step guide to setting up, configuring, and using FlaskBlog.
-
Open the terminal:
-
Enter the Python interactive shell:
$ python
-
Generate a secret key using the secrets module:
>>> import secrets >>> secrets.token_hex(16)
-
Copy the generated key and paste it in the
__init__.py
file in the place of "GENERATED_KEY"app.config['SECRET_KEY'] = 'GENERATED_KEY'
-
Save the file and run the application.
-
Navigate to the FlaskBlog directory in your terminal.
-
Launch the Python interactive shell:
$ python
-
Then Run the below commands to Initialize the database and create tables:
>>> from FlaskBlog import db, app >>> from FlaskBlog.models import User, Post >>> with app.app_context(): ... db.create_all() ...
-
Create a new user:
>>> with app.app_context(): ... new_user = User(username='username', email='email', password='password') ... db.session.add(new_user) ... db.session.commit() ...
-
Retrieve all users to verify the user creation:
>>> with app.app_context(): ... all_users = User.query.all() ... print(all_users)
-
Create a new post:
>>> with app.app_context(): ... new_post = Post(title='title', content='content', user_id=1) ... db.session.add(new_post) ... db.session.commit()
$ python
>>> from flask_bcrypt import Bcrypt
>>> bcrypt = Bcrypt()
>>> password = 'password'
>>> hashed_password = bcrypt.generate_password_hash(password).decode('utf-8')
>>> hashed_password
>>> bcrypt.check_password_hash(hashed_password, password)
$ python
-
Import Required Module:
>>> from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
-
Initialize Serializer:
>>> s = Serializer('secret', 30)
-
Generate Token:
>>> token = s.dumps({'user_id': 1}).decode('utf-8')
-
Access Token:
>>> token 'eyJhbGciOiJIUzUxMiIsImlhdCI6MTcxMzY0MTA4MSwiZXhwIjoxNzEzNjQxMTExfQ.eyJ1c2VyX2lkIjoxfQ.mIftgDnRcaCAfzp68k88QGO_i4ajWyqFGmbajLAm7p-ay3n8hxNTBZh1sbpXSe_lpMkLMsx74ybKOCKvOXjJpg'
-
Verify Token:
>>> s.loads(token) # Access within 30 seconds {'user_id': 1}