Build a Full-Featured Flask CMS with Admin Panel and RESTful API in One Hour
This tutorial walks you through creating a simple content management system using Flask, demonstrating how to set up the environment, define SQLAlchemy models, generate an admin interface with Flask‑Admin, and expose RESTful endpoints with Flask‑Restless, all within about an hour of development.
1. Introduction
Flask is a lightweight framework built on Werkzeug; it provides only request handling and template rendering without an integrated ORM, making it highly flexible for production use when combined with extensions.
This article shows how to build a simple CMS with an admin backend and API in about an hour, focusing on practical development rather than deep source‑code analysis.
2. Quick Start
The core of a CMS is articles and their categories.
2.1 Environment Setup
Install Flask and Flask‑SQLAlchemy:
pip install Flask Flask-SQLAlchemy2.2 Quick Demo
from flask import Flask
app = Flask(__name__)
@app.route('/')
def main():
return 'hello world'
if __name__ == '__main__':
app.run(debug=True, port=5012)This minimal "Hello World" example demonstrates Flask’s low entry barrier.
2.3 Model Creation
Configure SQLite and define models with SQLAlchemy:
from flask_sqlalchemy import SQLAlchemy
DB = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'SQLite://test.db'Model definitions:
class Category(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(48), nullable=False, doc='分类名')
pid = db.Column(db.Integer, nullable=False, index=True, doc='父ID')
def __repr__(self):
return '{} <{}>'.format(self.name, self.id)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(128), index=True, nullable=False, doc='标题')
category_id = db.Column(db.Integer, db.ForeignKey('category.id'), nullable=False, doc='分类ID')
content = db.Column(db.Text)
category = db.relationship('Category', backref='post')Create tables on the first request:
@app.before_first_request
def create_db():
db.create_all()3. Quickly Build Admin Backend
Install Flask‑Admin and configure it:
pip install Flask-Admin from flask_admin import Admin
admin = Admin(app, name='Baixing Simple CMS', template_mode='bootstrap3')Set a secret key for form submissions:
app.config['SECRET_KEY'] = '!!!-----www.baixing.com-----!!!!'Import ModelView and register the models:
from flask_admin.contrib.sqla import ModelView
admin.add_view(ModelView(Category, db.session))
admin.add_view(ModelView(Post, db.session))Visit http://127.0.0.1:5012/admin/ to see the auto‑generated CRUD interface, which can be customized via templates or custom views.
4. Quickly Build RESTful API
Install Flask‑Restless and generate GET‑only APIs for the models:
pip install Flask-Restless from flask_restless import APIManager
api_manager = APIManager(app, flask_sqlalchemy_db=db)
api_manager.create_api(Post, methods=['GET'])
api_manager.create_api(Category, methods=['GET'])This creates endpoints such as /api/post, /api/category, and /api/category/<id>/post, automatically handling pagination and relationship traversal.
5. Afterword
The combination of Flask, Flask‑SQLAlchemy, Flask‑Admin, and Flask‑Restless enables rapid development of internal systems or prototype products with minimal code while retaining extensibility for future refactoring.
Source code is available at https://gist.github.com/couldtt/22583f6f4cb303a29c2b004484afe9ab .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Baixing.com Technical Team
A collection of the Baixing.com tech team's insights and learnings, featuring one weekly technical article worth following.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
