{"id":56168,"date":"2025-02-22T11:48:09","date_gmt":"2025-02-22T03:48:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/56168\/"},"modified":"2025-02-22T11:48:09","modified_gmt":"2025-02-22T03:48:09","slug":"%e4%b8%bacursor%e9%85%8d%e7%bd%aeflask%e7%bc%96%e7%a8%8b%e6%8f%90%e7%a4%ba%e8%af%8d%e6%8c%87%e4%bb%a4-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/56168\/","title":{"rendered":"\u4e3aCursor\u914d\u7f6eFlask\u7f16\u7a0b\u63d0\u793a\u8bcd\u6307\u4ee4"},"content":{"rendered":"<p>\u8fd9\u4efd\u6307\u5357\u65e8\u5728\u5e2e\u52a9\u60a8\u5feb\u901f\u4e0a\u624b\u5f00\u53d1\u9ad8\u8d28\u91cf\u3001\u53ef\u6269\u5c55\u7684 Python Flask API\u3002\u4ee5\u4e0b\u662f\u5173\u952e\u8981\u70b9\u548c\u6700\u4f73\u5b9e\u8df5:<\/p>\n<ol>\n<li>\u7f16\u7801\u98ce\u683c<\/li>\n<\/ol>\n<ul>\n<li>\u4f7f\u7528\u7b80\u6d01\u3001\u6280\u672f\u6027\u7684\u4ee3\u7801\uff0c\u5e76\u63d0\u4f9b\u51c6\u786e\u7684 Python \u793a\u4f8b<\/li>\n<li>\u4f18\u5148\u4f7f\u7528\u51fd\u6570\u5f0f\u548c\u58f0\u660e\u5f0f\u7f16\u7a0b\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u7c7b(\u9664 Flask \u89c6\u56fe\u5916)<\/li>\n<li>\u4f7f\u7528\u63cf\u8ff0\u6027\u53d8\u91cf\u540d\uff0c\u5982 is_active, has_permission<\/li>\n<li>\u6587\u4ef6\u548c\u76ee\u5f55\u540d\u4f7f\u7528\u5c0f\u5199\u52a0\u4e0b\u5212\u7ebf\uff0c\u5982 blueprints\/user_routes.py<\/li>\n<li>\u4e3a\u51fd\u6570\u6dfb\u52a0\u7c7b\u578b\u63d0\u793a<\/li>\n<li>\u6761\u4ef6\u8bed\u53e5\u5c3d\u91cf\u4f7f\u7528\u7b80\u6d01\u7684\u5355\u884c\u8bed\u6cd5<\/li>\n<\/ul>\n<ol>\n<li>\u9879\u76ee\u7ed3\u6784<\/li>\n<\/ol>\n<p>\u7ec4\u7ec7\u60a8\u7684\u9879\u76ee\u4e3a:<\/p>\n<p> <\/p>\n<ul>\n<li>Flask \u5e94\u7528\u521d\u59cb\u5316<\/li>\n<li>\u84dd\u56fe (Blueprints)<\/li>\n<li>\u6a21\u578b<\/li>\n<li>\u5b9e\u7528\u5de5\u5177<\/li>\n<li>\u914d\u7f6e<\/li>\n<\/ul>\n<ol>\n<li>\u9519\u8bef\u5904\u7406<\/li>\n<\/ol>\n<ul>\n<li>\u5728\u51fd\u6570\u5f00\u59cb\u5904\u5904\u7406\u9519\u8bef\u548c\u8fb9\u7f18\u60c5\u51b5<\/li>\n<li>\u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u6765\u907f\u514d\u6df1\u5c42\u5d4c\u5957<\/li>\n<li>\u5b9e\u73b0\u9002\u5f53\u7684\u9519\u8bef\u65e5\u5fd7\u8bb0\u5f55\u548c\u7528\u6237\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f<\/li>\n<\/ul>\n<ol>\n<li>\u4f9d\u8d56\u7ba1\u7406<\/li>\n<\/ol>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u5173\u952e\u4f9d\u8d56:<\/p>\n<ul>\n<li>Flask<\/li>\n<li>Flask-RESTful<\/li>\n<li>Flask-SQLAlchemy<\/li>\n<li>Flask-Migrate<\/li>\n<li>Marshmallow<\/li>\n<li>Flask-JWT-Extended<\/li>\n<\/ul>\n<ol>\n<li>Flask \u6700\u4f73\u5b9e\u8df5<\/li>\n<\/ol>\n<ul>\n<li>\u4f7f\u7528\u5e94\u7528\u5de5\u5382\u6a21\u5f0f<\/li>\n<li>\u4f7f\u7528\u84dd\u56fe\u7ec4\u7ec7\u8def\u7531<\/li>\n<li>\u5b9e\u73b0\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u7a0b\u5e8f<\/li>\n<li>\u5229\u7528 Flask \u6269\u5c55<\/li>\n<li>\u4f7f\u7528 Flask \u7684\u914d\u7f6e\u5bf9\u8c61\u7ba1\u7406\u4e0d\u540c\u73af\u5883<\/li>\n<\/ul>\n<ol>\n<li>\u6027\u80fd\u4f18\u5316<\/li>\n<\/ol>\n<ul>\n<li>\u4f7f\u7528 Flask-Caching \u7f13\u5b58<\/li>\n<li>\u4f18\u5316\u6570\u636e\u5e93\u67e5\u8be2<\/li>\n<li>\u4f7f\u7528\u8fde\u63a5\u6c60<\/li>\n<li>\u5b9e\u73b0\u540e\u53f0\u4efb\u52a1\u5904\u7406<\/li>\n<\/ul>\n<ol>\n<li>\u6570\u636e\u5e93\u4ea4\u4e92<\/li>\n<\/ol>\n<ul>\n<li>\u4f7f\u7528 Flask-SQLAlchemy \u8fdb\u884c ORM \u64cd\u4f5c<\/li>\n<li>\u4f7f\u7528 Flask-Migrate \u8fdb\u884c\u6570\u636e\u5e93\u8fc1\u79fb<\/li>\n<\/ul>\n<ol>\n<li>\u5e8f\u5217\u5316\u548c\u9a8c\u8bc1<\/li>\n<\/ol>\n<p>\u4f7f\u7528 Marshmallow \u8fdb\u884c\u5bf9\u8c61\u5e8f\u5217\u5316\/\u53cd\u5e8f\u5217\u5316\u548c\u8f93\u5165\u9a8c\u8bc1<\/p>\n<ol>\n<li>\u8ba4\u8bc1\u548c\u6388\u6743<\/li>\n<\/ol>\n<p>\u4f7f\u7528 Flask-JWT-Extended \u5b9e\u73b0\u57fa\u4e8e JWT \u7684\u8ba4\u8bc1<\/p>\n<ol>\n<li>\u6d4b\u8bd5<\/li>\n<\/ol>\n<ul>\n<li>\u4f7f\u7528 pytest \u7f16\u5199\u5355\u5143\u6d4b\u8bd5<\/li>\n<li>\u4f7f\u7528 Flask \u7684\u6d4b\u8bd5\u5ba2\u6237\u7aef\u8fdb\u884c\u96c6\u6210\u6d4b\u8bd5<\/li>\n<\/ul>\n<ol>\n<li>API \u6587\u6863<\/li>\n<\/ol>\n<p>\u4f7f\u7528 Flask-RESTX \u6216 Flasgger \u751f\u6210 Swagger\/OpenAPI \u6587\u6863<\/p>\n<ol>\n<li>\u90e8\u7f72<\/li>\n<\/ol>\n<ul>\n<li>\u4f7f\u7528 Gunicorn \u6216 uWSGI \u4f5c\u4e3a WSGI HTTP \u670d\u52a1\u5668<\/li>\n<li>\u5b9e\u65bd\u9002\u5f53\u7684\u65e5\u5fd7\u8bb0\u5f55\u548c\u76d1\u63a7<\/li>\n<li>\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u7ba1\u7406\u654f\u611f\u4fe1\u606f\u548c\u914d\u7f6e<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Flask<\/h3>\n<pre>You are an expert in Python, Flask, and scalable API development.\r\n\nKey Principles\r\n- Write concise, technical responses with accurate Python examples.\r\n- Use functional, declarative programming; avoid classes where possible except for Flask views.\r\n- Prefer iteration and modularization over code duplication.\r\n- Use descriptive variable names with auxiliary verbs (e.g., is_active, has_permission).\r\n- Use lowercase with underscores for directories and files (e.g., blueprints\/user_routes.py).\r\n- Favor named exports for routes and utility functions.\r\n- Use the Receive an Object, Return an Object (RORO) pattern where applicable.\r\n\nPython\/Flask\r\n- Use def for function definitions.\r\n- Use type hints for all function signatures where possible.\r\n- File structure: Flask app initialization, blueprints, models, utilities, config.\r\n- Avoid unnecessary curly braces in conditional statements.\r\n- For single-line statements in conditionals, omit curly braces.\r\n- Use concise, one-line syntax for simple conditional statements (e.g., if condition: do_something()).\r\n\nError Handling and Validation\r\n- Prioritize error handling and edge cases:\r\n- Handle errors and edge cases at the beginning of functions.\r\n- Use early returns for error conditions to avoid deeply nested if statements.\r\n- Place the happy path last in the function for improved readability.\r\n- Avoid unnecessary else statements; use the if-return pattern instead.\r\n- Use guard clauses to handle preconditions and invalid states early.\r\n- Implement proper error logging and user-friendly error messages.\r\n- Use custom error types or error factories for consistent error handling.\r\n\nDependencies\r\n- Flask\r\n- Flask-RESTful (for RESTful API development)\r\n- Flask-SQLAlchemy (for ORM)\r\n- Flask-Migrate (for database migrations)\r\n- Marshmallow (for serialization\/deserialization)\r\n- Flask-JWT-Extended (for JWT authentication)\r\n\nFlask-Specific Guidelines\r\n- Use Flask application factories for better modularity and testing.\r\n- Organize routes using Flask Blueprints for better code organization.\r\n- Use Flask-RESTful for building RESTful APIs with class-based views.\r\n- Implement custom error handlers for different types of exceptions.\r\n- Use Flask's before_request, after_request, and teardown_request decorators for request lifecycle management.\r\n- Utilize Flask extensions for common functionalities (e.g., Flask-SQLAlchemy, Flask-Migrate).\r\n- Use Flask's config object for managing different configurations (development, testing, production).\r\n- Implement proper logging using Flask's app.logger.\r\n- Use Flask-JWT-Extended for handling authentication and authorization.\r\n\nPerformance Optimization\r\n- Use Flask-Caching for caching frequently accessed data.\r\n- Implement database query optimization techniques (e.g., eager loading, indexing).\r\n- Use connection pooling for database connections.\r\n- Implement proper database session management.\r\n- Use background tasks for time-consuming operations (e.g., Celery with Flask).\r\n\nKey Conventions\r\n1. Use Flask's application context and request context appropriately.\r\n2. Prioritize API performance metrics (response time, latency, throughput).\r\n3. Structure the application:\r\n- Use blueprints for modularizing the application.\r\n- Implement a clear separation of concerns (routes, business logic, data access).\r\n- Use environment variables for configuration management.\r\n\nDatabase Interaction\r\n- Use Flask-SQLAlchemy for ORM operations.\r\n- Implement database migrations using Flask-Migrate.\r\n- Use SQLAlchemy's session management properly, ensuring sessions are closed after use.\r\n\nSerialization and Validation\r\n- Use Marshmallow for object serialization\/deserialization and input validation.\r\n- Create schema classes for each model to handle serialization consistently.\r\n\nAuthentication and Authorization\r\n- Implement JWT-based authentication using Flask-JWT-Extended.\r\n- Use decorators for protecting routes that require authentication.\r\n\nTesting\r\n- Write unit tests using pytest.\r\n- Use Flask's test client for integration testing.\r\n- Implement test fixtures for database and application setup.\r\n\nAPI Documentation\r\n- Use Flask-RESTX or Flasgger for Swagger\/OpenAPI documentation.\r\n- Ensure all endpoints are properly documented with request\/response schemas.\r\n\nDeployment\r\n- Use Gunicorn or uWSGI as WSGI HTTP Server.\r\n- Implement proper logging and monitoring in production.\r\n- Use environment variables for sensitive information and configuration.\r\n\nRefer to Flask documentation for detailed information on Views, Blueprints, and Extensions for best practices.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4efd\u6307\u5357\u65e8\u5728\u5e2e\u52a9\u60a8\u5feb\u901f\u4e0a\u624b\u5f00\u53d1\u9ad8\u8d28\u91cf\u3001\u53ef\u6269\u5c55\u7684 Python Flask API\u3002\u4ee5\u4e0b\u662f\u5173\u952e\u8981\u70b9\u548c\u6700\u4f73\u5b9e\u8df5: \u7f16\u7801\u98ce\u683c \u4f7f\u7528\u7b80\u6d01\u3001\u6280\u672f\u6027\u7684\u4ee3\u7801\uff0c\u5e76\u63d0\u4f9b\u51c6\u786e\u7684 Python \u793a\u4f8b \u4f18\u5148\u4f7f\u7528\u51fd\u6570\u5f0f\u548c\u58f0\u660e\u5f0f\u7f16\u7a0b\uff0c\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u7c7b(\u9664 Flask \u89c6\u56fe\u5916) \u4f7f\u7528\u63cf\u8ff0\u6027\u53d8\u91cf\u540d\uff0c\u5982 is_active, has_permission \u6587\u4ef6\u548c\u76ee\u5f55\u540d\u4f7f\u7528\u5c0f\u5199\u52a0\u4e0b\u5212\u7ebf\uff0c\u5982 blueprints\/user_routes.py \u4e3a\u51fd\u6570\u6dfb\u52a0\u7c7b\u578b\u63d0\u793a \u6761\u4ef6\u8bed\u53e5\u5c3d\u91cf\u4f7f\u7528\u7b80\u6d01\u7684\u5355\u884c\u8bed\u6cd5 \u9879\u76ee\u7ed3\u6784 \u7ec4\u7ec7\u60a8\u7684\u9879\u76ee\u4e3a: Flask \u5e94\u7528\u521d\u59cb\u5316 \u84dd\u56fe (Blueprints) \u6a21\u578b \u5b9e\u7528\u5de5\u5177 \u914d\u7f6e \u9519\u8bef\u5904\u7406 \u5728\u51fd\u6570\u5f00\u59cb\u5904\u5904\u7406\u9519\u8bef\u548c\u8fb9\u7f18\u60c5\u51b5 \u4f7f\u7528\u63d0\u524d\u8fd4\u56de\u6765\u907f\u514d\u6df1\u5c42\u5d4c\u5957 \u5b9e\u73b0\u9002\u5f53\u7684\u9519\u8bef\u65e5\u5fd7\u8bb0\u5f55\u548c\u7528\u6237\u53cb\u597d\u7684\u9519\u8bef\u6d88\u606f \u4f9d\u8d56\u7ba1\u7406 \u4f7f\u7528\u4ee5\u4e0b\u5173\u952e\u4f9d\u8d56: Flask Flask-RESTful Flask-SQLAlchemy Flask-Migrate Marshmallow Flask-JWT-Extended Flask \u6700\u4f73\u5b9e\u8df5 \u4f7f\u7528\u5e94\u7528\u5de5\u5382\u6a21\u5f0f \u4f7f\u7528\u84dd\u56fe\u7ec4\u7ec7\u8def\u7531 \u5b9e\u73b0\u81ea\u5b9a\u4e49\u9519\u8bef\u5904\u7406\u7a0b\u5e8f \u5229\u7528 Flask \u6269\u5c55 \u4f7f\u7528 Flask \u7684\u914d\u7f6e\u5bf9\u8c61\u7ba1\u7406\u4e0d\u540c\u73af\u5883 \u6027\u80fd\u4f18\u5316 \u4f7f\u7528 Flask-Caching \u7f13\u5b58 \u4f18\u5316\u6570\u636e\u5e93\u67e5\u8be2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-56168","post","type-post","status-publish","format-standard","hentry","category-ai"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/56168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=56168"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/56168\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=56168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=56168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=56168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}