Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    React

    Svelte for React Developers

    Programming

    20. Node.js Lessons. Data Streams in Node.JS, fs.ReadStream

    Programming

    DigitalOcean vs. AWS: Comparing Offers and Choosing the Better Option

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Saturday, September 6
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / JavaScript / How to Create a Personal Blogging Website: Back-End (Flask/Python) #1
    Angular

    How to Create a Personal Blogging Website: Back-End (Flask/Python) #1

    Vivek BishtBy Vivek BishtFebruary 11, 2020Updated:March 3, 2020No Comments12 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    How to Create a Personal Blogging Website: Back-End (Flask/Python) #1
    Create a Blog with Angular and Flask
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Create a Blog with Angular and Flask
    Create a Blog with Angular and Flask

    The first thing which comes in our mind when we think of creating a personal blogging website is to use any CMS to do most of the work for you. Although content management systems provide a quick and easy way to set up a blogging website, adding custom features and look to one’s website becomes tricky.

    For web developers like me, it’s always exciting to code a blog since it gives me complete authority over my website.

    Yes, the process of creating a personal blogging website from scratch can be tedious, but also, especially as a newbie developer, it can be a great learning experience.

    In this article, we’re going to create the back-end for our application using Flask, and in the next part, we’ll be creating the front-end using Angular.

    Requirements for the back-end:

    1. Python (I’m using version 3.8.0)
    2. Text-editor (I’m using visual studio code)

    Create your application directory, and add folders and files in your application directories so that the application directory looks like this:

    app directory
    app directory

    Next, we’re going to create our back-end in the following steps:

    1. Flask set up
    2. Creating tables
    3. Adding routes
    4. Protecting routes

    Flask set up

    For setting up Flask,

    Inside your command prompt/terminal, run the following commands:

    pipenv install flask flask-sqlalchemy flask-cors

    The above command will create a virtual environment for our project and also install flask and flask-sqlalchemy.

    Note- For those of you who aren’t aware of flask-sqlalchemy, in simple terms, flask-sqlalchemy provides us support to perform CRUD operations on the database without having to write raw SQL statements.

    To activate the virtual environment, all you have to do is to run the following command:

    pipenv shell

    Inside api/__init__.py add the following code:

    from flask import Flask
    from flask_sqlalchemy import SQLAlchemy
    from flask_cors import CORS
    
    db = SQLAlchemy()
    
    def create_app():
        app = Flask(__name__)
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///flaskdatabase.db'
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
        CORS(app)
    
        db.init_app(app)
    
        return app

    The code above is quite straight forward, we’ve created a function called create_app() which is our application factory and it initializes our app and as well as our database.

    Creating tables

    We’re going to create a blog table where we store all the properties of a blog. The properties/columns I’m considering for this particular blog are as follows:

    1. id ( This will be our blog-id)
    2. title (Title of the blog)
    3. content (Content of the blog)
    4. created_at (Date at which the blog was created)
    5. feature_image (Cover image for our blog)
    6. tags (Tags related to the blog)

    One can add more properties/columns to the blog according to their convenience.

    Let’s start building the model for our table.

    Inside Blog/blog_model.py add the following code:

    from api import db
    from datetime import datetime
    from api.Tags_Blog.tag_blog_table import tag_blog
    
    class Blog(db.Model):
        id = db.Column(db.Integer,primary_key=True)
        title=db.Column(db.String(50),nullable=False)
        content=db.Column(db.Text,nullable=False)
        feature_image= db.Column(db.String,nullable=False)
        created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
        @property
        def serialize(self):
            return {
                'id': self.id,
                'title': self.title,
                'content': self.content,
                'feature_image': self.feature_image,
                'created_at': self.created_at,
            }

    We’ve created a new Blog Model and we’ve added the properties that our blog will contain. Also, we’ve added a function named serialize so that we can return data in JSON format.

    Notice that we’ve haven’t added the tags property to our blog model. The reason behind not adding the tags property yet is,  first 5 properties of the blog are pretty straight forward, however, tags property of the blog should be handled differently since the tag property maintains a many-to-many relationship with the blog i.e each blog can have multiple numbers of tags and each tag can be added to multiple numbers of blogs.

    For handling many-to-many relationships, we need to create two tables, one for storing the Tag itself and the other will be helper table for storing the reference for the blog and tag i.e this particular table will contain just two columns, tag_id and blog_id and these two columns will act as Foreign Keys to the tables Tag and Blog respectively.

    Let’s create the first table called Tag, which will contain all the tags that we create.

    Inside api/Tag/tag_model.py, add the following code

    from api import db
    
    class Tag(db.Model):
        id=db.Column(db.Integer,primary_key=True)
        name=db.Column(db.String(20))
        @property
        def serialize(self):
            return {
            'id': self.id,
            'name': self.name     
            }

    Next, let’s create a table for storing the reference of the blog and the tag.

    As mentioned in the official flask-sqlalchemy docs, we needn’t create a Model for this table.

    Inside api/Tags_Blog/tag_blog_table.py, add the following code:

    from api import db
    
    tag_blog = db.Table('tag_blog',
        db.Column('tag_id',db.Integer,db.ForeignKey('tag.id'), primary_key=True),
        db.Column('blog_id', db.Integer,db.ForeignKey('blog.id'),primary_key=True)
    )

    As one can see from the above code, we’ve created a table to store tag_id and blog_id respectively. Notice that I’ve defined the columns as Primary Keys, this way, a tag can be mentioned only once per blog.

    Now that we’ve defined both the tables, we can add the tags property inside our Blog model:

    tags=db.relationship('Tag',secondary=tag_blog,backref=db.backref('blogs_associated',lazy="dynamic"))

    Note- Don’t forget to import the tag_blog table inside blog_model.py.

    Unlike the other properties in the blog model, tags property is not a column in our database. It’s there to create a connection between Blog, Tag,  and the tag_blog table. In the above line of code, we’ve specified the relationship between Blog, Tag and tag_blog table. Also, in the above line of code, we created a new property inside the Tag table called blogs_associated which can be used to describe which blogs are associated with a single tag.

    Read More:  Implementing Search Functionality with Meilisearch, Prisma and Express

    Note- Remember that tags and blogs_associated properties aren’t columns in our database since they are attributes to describe the many-to-many relationship. Although they aren’t columns, we can still query both the attributes.

    Adding routes

    Before adding the routing methods to our application, we need to create a blueprint inside blog_routes.py and import it in the __init__.py file.

    Note- For those of you who aren’t aware of blueprints, they provide us a way for organizing our application into smaller reusable components. In this case, we are creating a blueprint for our blog so that we define all the routes related to the blog inside the blueprint.

    Inside Blog/blog_routes.py add the following code:

    from flask import Blueprint
    
    from api import db
    
    blogs= Blueprint('blogs',__name__)

    Next, we need to register this blueprint in our application, so inside the create_app function of the __init__.py file, add the following code:

    from api.Blog.blog_routes import blogs
    app.register_blueprint(blogs)

    Let’s add routing to our application for performing CRUD operations on the database,

    Creating:

    Add the following code inside blog_routes.py:

    @blogs.route('/add_blog',methods=["POST"])
    def create_blog():
        data = request.get_json()
    
        new_blog=Blog(title=data["title"],content=data["content"],feature_image=data["feature_image"])
    
        for tag in data["tags"]:
            present_tag=Tag.query.filter_by(name=tag).first()
            if(present_tag):
                present_tag.blogs_associated.append(new_blog)
            else:
                new_tag=Tag(name=tag)
                new_tag.blogs_associated.append(new_blog)
                db.session.add(new_tag)
                
    
        db.session.add(new_blog)
        db.session.commit()
    
        blog_id = getattr(new_blog, "id")
        return jsonify({"id": blog_id})

    Note- Don’t forget to import “jsonify” and “request” from flask on top of the page

    Let’s breakdown the code we’ve added,

    We’ve created a new route called add_blog and this route will be handled by a function called create_blog.

    The create_blog function will first parse the incoming JSON data from the request. Then, it creates a new instance of our blog model from the data received from the request.

    Next, we’re going to send tags as an “Array” inside the body of the request, so we need to find a way to check if each tag specified in the array is already present in the Tag table and if it isn’t present, we need to create a new Tag. For this purpose, I’ve created a “for” loop which iterates through each tag present in the tags array of the request and performs actions accordingly.

    Retrieving:

    We’re going to create two routes for retrieving operation, one for retrieving all the blogs from the database and the other for retrieving a single blog by id.

    For retrieving all the blogs from the database, add the following code inside blog_routes.py:

    @blogs.route('/blogs',methods=["GET"])
    def get_all_blogs():
        blogs= Blog.query.all()
        serialized_data = []
        for blog in blogs:
            serialized_data.append(blog.serialize)
    
        return jsonify({"all_blogs": serialized_data})

    In the above code, we query the database to retrieve all the blogs. Since we cannot directly send the blog object in the return function, we use the serialize function to convert the blog object into JSON format and then return all the blogs.

    For retrieving a single blog by id, add the following code inside blog_routes.py:

    @blogs.route('/blog/<int:id>',methods=["GET"])
    def get_single_blog(id):
        blog = Blog.query.filter_by(id=id).first()
        serialized_blog = blog.serialize
        serialized_blog["tags"] = []
    
        for tag in blog.tags:
            serialized_blog["tags"].append(tag.serialize)
    
        return jsonify({"single_blog": serialized_blog})

    In the above code, we query the database based on the id provided in the URL. Also, since I want to return the blog object with tags property as well, I’ve appended all the tags present inside blog.tags to the serialized_blog[“tags”] and then returned the serialized_blog.

    Updating:

    For updating the database, I’m using the PUT method.

    Inside blog_routes.py, add the following code:

    @blogs.route('/update_blog/<int:id>', methods=["PUT"])
    def update_blog(id):
        data = request.get_json()
        blog=Blog.query.filter_by(id=id).first_or_404()
    
        blog.title = data["title"]
        blog.content=data["content"]
        blog.feature_image=data["feature_image"]
    
        updated_blog = blog.serialize
    
        db.session.commit()
        return jsonify({"blog_id": blog.id})
    

    In the code above, inside the update_blog function, we query the database based on the id provided in the url and find the record. Then we replace the information present in the record with data provided inside the body of the request.

    Deleting:

    The record deletion route is quite straight forward:

    @blogs.route('/delete_blog/<int:id>', methods=["DELETE"])
    def delete_blog(id):
        blog = Blog.query.filter_by(id=id).first()
        db.session.delete(blog)
        db.session.commit()
    
        return jsonify("Blog was deleted"),200

    Protecting routes

    We’re going to create an admin user for our application and only the admin user will have access to all the routes, therefore, we need to protect some of our routes from a non-admin user.

    Before adding functionality to protect our routes, let’s create an admin user.

    We need to create a new User model in our application, so inside api/User/user_model.py, add the following code:

    from api import db
    
    class User(db.Model):
        id=db.Column(db.Integer,primary_key=True)
        email=db.Column(db.String(120),nullable=False)
        password=db.Column(db.String(120),nullable=False)
    

    Since we are going to create a single admin user for the application, we needn’t create routes for the User model. We’re going to create a custom admin user just before our server starts. For doing so, we are going to create a custom command which will be executed right before we run the flask run command and this custom command will create an admin user for us.

    Read More:  Upload Multiple Images to a Django Model without plugins

    Inside the create_app function of __init__.py file, add the following code:

    @click.command(name='create_admin')   
        @with_appcontext
        def create_admin():
            admin=User(email="admin_email_address",password="admin_password")
            admin.password = generate_password_hash(admin.password,'sha256',salt_length=12)
            db.session.add(admin)
            db.session.commit()
    
        app.cli.add_command(create_admin)

    Note: Don’t forget to add the following import statements on top of the page:

    import click
    from flask.cli import with_appcontext
    from werkzeug.security import generate_password_hash

    Let’s breakdown the code.

    We created a custom command called create_admin. Next, we added our app instance to this command by using @with_appcontext. The custom command will execute a function create_admin. The function creates a new instance of the user model with the specified email and password. Since for security purposes, we cannot store the password as a string, therefore, we generate a hash and then store the hashed password in the database.  Next, we add the create_admin command to the app cli.

    Note- Don’t run the command create_admin yet since we haven’t created the database yet.

    Now that we’ve created our custom admin user, we need to create a login route where we match the input credentials with our admin credentials and if they match, we’re going to send back a JWT token in the response.

    Inside Login/login_route.py, add the following code:

    from flask import Blueprint,request,jsonify
    from api.User.user_model import User
    from flask_jwt_extended import create_access_token
    from werkzeug.security import check_password_hash 
    
    login=Blueprint('login', __name__)
    
    @login.route('/login', methods=["POST"])
    def log_in():
        request_data = request.get_json()
    
        user=User.query.filter_by(email=request_data["email"]).first()
        if user:
            if check_password_hash(user.password,request_data["password"]):
                jwt_token=create_access_token(identity=user.email)
                return jsonify({"token":jwt_token})
        else:
            return "Invalid email or password",400

    In the above route, we validate the credentials and if they match, we’re sending back a JWT token in the response.

    Also, we need to set up the flask-jwt-extended library for our application. For doing so, add the following lines of code inside create_app function of the __init__.py file:

    app.config['JWT_SECRET_KEY']='YOUR_SECRET_KEY'
    jwt=JWTManager(app)

    Remember to change the YOUR_SECRET_KEY value with a value of your choice. Also, add the import statement to import JWTManager from flask-jwt-extended.

    Note- Don’t forget to install flask-jwt-extended library and with the command:

    pipenv install flask-jwt-extended

    Now that we’ve created our login route, with the help of flask-jwt-extended, by just adding one line of code we can protect specific routes in our application.
    For doing so, add code inside the routes so that the protected route looks like this:

    @blogs.route('/delete_blog/<int:id>', methods=["DELETE"])
    @jwt_required
    def delete_blog(id):
        blog = Blog.query.filter_by(id=id).first()
        db.session.delete(blog)
        db.session.commit()
    
        return jsonify("Blog was deleted"),200

    Notice that I’ve added the line @jwt_required inside the route. By doing this, this route cannot be accessed without providing a valid JWT token inside the header. Add the same line of code for the following routes:

    1. add_blog
    2. update_blog

    Note- Don’t forget to import jwt_required from flask-jwt-extended library.

    To access the protected route, one needs to provide an authorization header in the following format:

    Bearer ‘YOUR_JWT_TOKEN‘

    With this, we’ve completed protecting some of our routes.

    Before creating our database and running our application, we need to remember a few points:

    1. Import all the models created inside the create_app function of the __init__.py file
    2. Since the application is in the development phase, I’ve stored the admin user and the JWT_SECRET_KEY in our code. Changes should be made when the application is in the deployment phase.
    3. Error handling of the routes hasn’t been done in this article since each developer has a different approach to error handling.

    We haven’t created the database for this application. So for doing so, run the following commands inside the virtual environment:

    python
    from api import db,create_app
    db.create_all(app=create_app())
    

    This should create a database called flaskdatabase.db in your application directory.

    Next, type the following commands to run the application:

    set FLASK_APP=api
    set FLASK_DEBUG=1
    set FLASK_ENV=development
    flask create_admin
    flask run

    By running the above commands, we’ve set up some environment variables for our application, we’ve run the custom command to create the admin user for our application and then, we ran the application using the flask run command.

    Note- The above commands are for Windows users, for Linux and Mac users, replace set with export.

    There you go, we’ve created the back-end part for our personal blogging website. In the next part, we’ll add the client-side code to complete the application.

    Below is the link to the repository of the back-end:

    https://github.com/Bviveksingh/Personal-blog-flask

    Second part: How to Create a Personal Blogging Website: Front-End (Angular) #2

    flask
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Vivek Bisht

      Related Posts

      Streamlining Resource Allocation for Enhanced Project Success

      December 18, 2024

      Conducting Effective Post-Project Evaluations: A Guide

      December 16, 2024

      Strategies for Keeping Projects on Track and Meeting Deadlines

      December 10, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Beginners October 2, 2020

      Node.js Lesson 6: Util and Inheritance

      This lesson and the coming one will cover some of the modules that we generally use in development. One of the modules is util and it comes very handy to ease the developer work while debugging. Let’s talk more about it by answering the following questions.

      Navigating International Hiring: Solutions for Recruitment Challenges

      November 26, 2024

      How to Build Real-Time Applications Using Node.js and WebSockets

      May 27, 2023

      Sending Emails in Java Applications: A Comprehensive Guide to JavaMail and SMTP Configuration

      January 9, 2024

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      Ways to optimize React applications

      JavaScript July 24, 2020

      Top Front-End Development Tools to Elevate Your Projects in 2024

      Development November 27, 2024

      Building Web Apps with Vue 3 composition API + Typescript + Vuex(4.0)

      Vue December 3, 2020

      Interview with Sergey

      Interview October 21, 2018

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Beginners

      Data Science Overview: The Bigger Picture

      LinkedIn

      Transforming LinkedIn Connections into Effective Sales Leads

      Trends

      7 Job Recruitment Trends 2019 | From Self-Service Modules to Peculiarities of Gen Z

      Most Popular

      Markdown Cheat Sheet: Definitive Guide to Markdown + Markdown Resources

      Beginners

      Interview with “Soshace” team

      Interview

      7 Concepts: Why Your SVG Is the Way It Is

      Beginners
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.