# Python学习平台项目后端 **Repository Path**: wangs_knight/python-learning-platform-backend ## Basic Information - **Project Name**: Python学习平台项目后端 - **Description**: No description available - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-05-22 - **Last Updated**: 2026-05-22 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Python学习平台 - 后端 ## 🚀 项目概述 Python学习平台的后端服务,基于Django REST Framework构建,提供RESTful API接口,支持用户认证、练习管理、学习资料、提交记录等功能。 ## 📐 技术栈 - **框架**: Django 4.2 + Django REST Framework - **数据库**: MySQL 8.0 - **认证**: JWT (JSON Web Token) - **文档**: RESTful API ## 🗄️ 数据库设计 ### 核心表结构 ```sql -- 用户表 users (id, username, email, nickname, avatar, bio, learning_level, total_score, completed_exercises, created_at, updated_at, is_active, is_admin) -- 分类表 categories (id, name, description, icon, sort_order, created_at) -- 练习表 exercises (id, title, description, difficulty, category_id, starter_code, expected_output, test_cases, hints, time_limit, memory_limit, points, views_count, attempts_count, success_count, created_by, created_at, updated_at, is_published) -- 学习资料表 materials (id, title, description, content, category_id, difficulty, author, cover_image, reading_time, likes_count, views_count, created_by, created_at, updated_at, is_published) -- 提交记录表 submissions (id, user_id, exercise_id, code, language, status, output, error_message, execution_time, memory_usage, test_results, ip_address, created_at, updated_at) -- 学习记录表 learning_records (id, user_id, exercise_id, material_id, action_type, duration, score, metadata, created_at) -- 用户统计表 user_stats (id, user_id, total_exercises, completed_exercises, total_score, total_submissions, success_rate, last_activity) -- 评论表 comments (id, user_id, exercise_id, material_id, parent_id, content, likes_count, is_deleted, created_at, updated_at) -- 用户成就表 achievements (id, user_id, achievement_type, achievement_name, achievement_icon, description, earned_at) -- 用户进度表 user_progress (id, user_id, exercise_id, status, attempts_count, best_score, last_attempt_at, completed_at) -- 学习会话表 study_sessions (id, user_id, start_time, end_time, duration, exercises_completed, score_earned, ip_address) -- 通知表 notifications (id, user_id, title, content, notification_type, is_read, created_at, metadata) ``` ## 🌐 API接口设计 ### 认证接口 ``` POST /api/auth/register/ # 用户注册 POST /api/auth/login/ # 用户登录 POST /api/auth/logout/ # 用户登出 POST /api/auth/refresh/ # 刷新Token GET /api/auth/profile/ # 获取用户信息 PUT /api/auth/profile/ # 更新用户信息 ``` ### 练习接口 ``` GET /api/exercises/ # 获取练习列表 GET /api/exercises/{id}/ # 获取练习详情 POST /api/exercises/ # 创建练习(管理员) PUT /api/exercises/{id}/ # 更新练习(管理员) DELETE /api/exercises/{id}/ # 删除练习(管理员) GET /api/exercises/categories/ # 获取分类列表 GET /api/exercises/popular/ # 获取热门练习 ``` ### 学习资料接口 ``` GET /api/materials/ # 获取资料列表 GET /api/materials/{id}/ # 获取资料详情 POST /api/materials/ # 创建资料(管理员) PUT /api/materials/{id}/ # 更新资料(管理员) DELETE /api/materials/{id}/ # 删除资料(管理员) GET /api/materials/popular/ # 获取热门资料 POST /api/materials/like/{id}/ # 点赞资料 ``` ### 提交记录接口 ``` POST /api/submissions/ # 提交代码 GET /api/submissions/{id}/ # 获取提交详情 GET /api/submissions/user/ # 获取用户提交记录 GET /api/submissions/stats/ # 获取提交统计 ``` ### 学习记录接口 ``` GET /api/learning/ # 获取学习记录 GET /api/learning/stats/ # 获取学习统计 POST /api/learning/action/ # 记录学习行为 GET /api/learning/recent/ # 获取最近活动 GET /api/learning/progress/ # 获取学习进度 ``` ### 管理接口 ``` GET /api/admin/users/ # 获取用户列表 GET /api/admin/users/{id}/ # 获取用户详情 POST /api/admin/users/{id}/toggle/ # 切换用户状态 POST /api/admin/users/{id}/grant-admin/ # 授予管理员权限 POST /api/admin/users/{id}/revoke-admin/ # 撤销管理员权限 ``` ## 🚀 快速开始 ### 1. 环境准备 ```bash # 创建虚拟环境 python -m venv venv # 激活虚拟环境 source venv/bin/activate # Linux/Mac venv\Scripts\activate # Windows # 安装依赖 pip install -r requirements.txt ``` ### 2. 数据库配置 ```bash # 配置数据库连接 cp .env.example .env # 编辑.env文件,填入数据库连接信息 # 创建数据库 mysql -u root -p -e "CREATE DATABASE pythonLN CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" # 运行迁移 python manage.py makemigrations python manage.py migrate # 创建超级用户 python manage.py createsuperuser ``` ### 3. 创建测试数据 ```bash python create_test_data.py ``` ### 4. 启动服务 ```bash # 启动开发服务器 python manage.py runserver 0.0.0.0:8000 # 或使用启动脚本 python start_server.py ``` ### 5. 访问地址 - **API服务**: http://localhost:8000/api - **管理后台**: http://localhost:8000/admin ## 🔧 配置说明 ### 数据库配置 在 `.env` 文件中配置数据库连接: ```env DB_HOST=49.7.207.170 DB_PORT=3369 DB_NAME=pythonLN DB_USER=pythonLN DB_PASSWORD=aKf53iE224xfeGeD ``` ### JWT配置 ```env SECRET_KEY=your-secret-key-here ACCESS_TOKEN_LIFETIME=60 REFRESH_TOKEN_LIFETIME=86400 ``` ### CORS配置 ```env ALLOWED_HOSTS=localhost,127.0.0.1,0.0.0.0 CORS_ALLOWED_ORIGINS=http://localhost:5173,http://localhost:3000 ``` ## 📊 功能特性 ### 🎯 核心功能 1. **用户系统** - 注册、登录、登出 - 个人资料管理 - 学习进度跟踪 - 成就系统 2. **练习系统** - 在线代码编辑 - 实时代码执行 - 自动测试验证 - 难度分级 - 积分系统 3. **学习资料** - 文章阅读 - 分类管理 - 点赞功能 - 阅读统计 4. **管理后台** - 用户管理 - 内容管理 - 数据统计 - 系统设置 ### 🔧 技术特性 - **RESTful API**: 标准化接口设计 - **JWT认证**: 安全的身份验证 - **CORS保护**: 跨域请求控制 - **SQL注入防护**: Django ORM保护 - **XSS防护**: 前端输入过滤 - **CSRF保护**: Django CSRF保护 ## 🔒 安全特性 - **密码加密**: 使用bcrypt加密 - **JWT认证**: 无状态认证 - **CORS保护**: 跨域请求控制 - **SQL注入防护**: Django ORM保护 - **XSS防护**: 前端输入过滤 - **CSRF保护**: Django CSRF保护 ## 📝 开发日志 ### v1.0.0 (2026-01-22) - ✅ 完成后端API架构 - ✅ 实现用户认证系统 - ✅ 实现练习管理系统 - ✅ 实现学习资料系统 - ✅ 实现提交记录系统 - ✅ 实现学习统计系统 - ✅ 实现管理后台功能 - ✅ 集成MySQL数据库 - ✅ 配置JWT认证 - ✅ 配置CORS跨域 - ✅ 创建测试数据 ## 📞 技术支持 如有问题,请查看以下资源: - [Django文档](https://docs.djangoproject.com/) - [Django REST Framework文档](https://www.django-rest-framework.org/) - [MySQL文档](https://dev.mysql.com/doc/) ## 📝 许可证 MIT License Copyright (c) 2026 Python学习平台 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.