Project architecture reconstruction (Blueprints): Turn 5000 lines of noodle code into Lego blocks
📂 Stage: Stage 4 - Practical Exercise (Daoman Blog) 🔗 Related chapters: 路由(Routing)艺术 · Flask-Login 实战
1. From "single file fun" to "refactoring nightmare"
When many people first come into contact with Flask, they will be attracted by a scene: Create a newapp.py, type a dozen lines of code, refresh the browser and you will see Hello World - this zero-configuration, instant feedback experience is exactly the charm of Flask's "micro-framework" positioning.
But this pleasure often doesn't last long. As more and more pages are added - homepage, login and registration, article publishing, background management, API interface, error handling...app.pywill irreversibly swell into a 5000+ lines of spaghetti code:
At this time, what you need is Blueprints, the modular architecture tool officially provided by Flask.
2. What is the blueprint? Not a drawing software!
Simply put, a blueprint is a reusable "function module container" in Flask.
It can mix the originalapp.pyThe routes, templates, static files, error handling and even custom filters are all packaged into independent folders. Finally, these modules are connected to the core Flask application through an "application factory", just like putting together Lego blocks.
The Daoman blog directory reconstructed with the blueprint will change from "flat chaos" to a "clearly layered" Lego-like structure:
💡 Note: under each blueprint
templates/In the folder, there must be one more nested subdirectory with the same name as the blueprint (such asmain/、auth/), otherwise conflicts will occur when there are templates with the same name under multiple blueprints.
3. Step by step: Create and stitch together the first blueprint
Let’s start with the simplest main function blueprint (main_bp) and quickly run through the entire process.
3.1 Define blueprint instance
for each blueprint module__init__.py, is its "ID card". Here we set the blueprint name, file path, and optional URL prefix.
3.2 Write main function route
Routes are now no longer directly decoratedapp.route, instead use Blueprint instance'sroute:
3.3 Register blueprint with application factory
Next is the core step of reconstruction: convert the original directly createdapp = Flask(__name__)Change it to a function - Application Factory (create_app). The advantage of this is that you can create applications with different configurations in different environments (development/test/production), and you can also register blueprints flexibly.
3.4 Write the simplest startup file
At this point, start the filerun.pyIt becomes very concise:
4. Advanced: Prefixed certification and article blueprint
4.1 Certification Blueprint (unified plus/authprefix)
Unifiedly add authentication-related routes/authThe prefix not only makes the URL look more standardized, but also facilitates unified adjustment in the future.
💡 Important reminder: After using the blueprint, **all
url_forAll must be prefixed with the blueprint identifier **. Even if it is an internal jump in the same blueprint, it is recommended to write it in full to maintain consistency.
4.2 Article blueprint (business logic with paging and permissions)
Although the business logic of the article blueprint is complex, it is still clear and easy to read after splitting:
5. Blueprint quick review and best practices
5.1 Core API Quick Check
5.2 Pitfall avoidance guidelines and best practices
-
Always import blueprints inside the application factory This is the only reliable way to avoid circular imports. Don't use it at the top of the file
from app.auth import auth_bpThis way of writing. -
Template/static files in the blueprint must be added to a subdirectory The template path should be written as
templates/main/index.htmlrather thantemplates/index.html, otherwise multiple blueprints will overwrite each other if they have templates with the same name. -
**
url_forMust be prefixed with blueprint identification ** Even if you jump within the same blueprint, it is recommended to writeurl_for("articles.detail", post_id=post.id), keep the code style consistent and avoid potential conflicts. -
Split the blueprint by function, not by technology A good division is "user module" and "article module" rather than "routing module" and "template module". The former is closer to the business, while the latter will only create difficulties in understanding.
-
Complex blueprints can continue to be split into sub-modules If a certain module is too large (such as backend management), it can be further split into multiple sub-blueprints such as "article management", "user management" and "configuration management", while still maintaining clarity.
6. Summary
This article aims at the maintenance bottleneck caused by Flask single-file development, and uses blueprints to disassemble Daoman Blog into Lego-style reusable modules:
- Directory reconstruction from “noodle code” to “layered architecture”
- Creation and routing binding of three core blueprints: main function, authentication, and article
- Implementation of application factory functions and blueprint registration
- Blueprint core API quick check and pitfall avoidance guide
Now you can safelyarticlesAdd comment function to the module, inauthThe module is connected to a third-party login, so there is no need to worry that changes will affect the code of other modules!
🔗 Extended reading

