Files
chengshishouce/INIT.md
mashen c866e74ece Initial commit: React + Django 城市手册项目
- Django 4.2 + DRF + JWT + GraphQL
- React 18 + MobX + styled-components
- PostgreSQL 数据库
- Docker + Docker Compose + Nginx
- 完整的功能模块(用户、版块、文章、服务、交互、版主管理)
- 完整的文档(需求、部署、测试)
2026-04-09 13:56:02 +00:00

120 lines
2.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 项目初始化指南
## 快速开始
### 1. 配置环境变量
```bash
# 复制环境变量模板
cp .env.example .env
# 编辑 .env 文件,配置以下内容:
# - DJANGO_SECRET_KEY (生成一个安全的密钥)
# - DB_NAME, DB_USER, DB_PASSWORD, DB_HOST (数据库配置)
# - ALLOWED_HOSTS (允许的域名)
```
### 2. 后端初始化
```bash
cd backend
# 创建虚拟环境
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
# 安装依赖
pip install -r requirements.txt
# 配置自定义用户模型 (首次运行需要)
# 在运行迁移前,确保 apps.users.models.User 已创建
# 运行迁移
python manage.py migrate
# 创建超级用户
python manage.py createsuperuser
# 运行开发服务器
python manage.py runserver
```
后端将在 http://localhost:8000 启动
### 3. 前端初始化
```bash
cd frontend
# 安装依赖
npm install
# 配置环境变量
cp .env.example .env
# 编辑 .env设置 REACT_APP_API_URL
# 启动开发服务器
npm start
```
前端将在 http://localhost:3000 启动
## Docker 部署
```bash
# 构建并启动所有服务
docker-compose up -d
# 查看日志
docker-compose logs -f
# 停止服务
docker-compose down
# 停止并删除卷
docker-compose down -v
```
## 数据库配置
### 使用外部 PostgreSQL
`.env` 中配置:
```env
DB_HOST=your-db-host
DB_PORT=5432
DB_NAME=your_database_name
DB_USER=your_database_user
DB_PASSWORD=your_database_password
```
### 使用 Docker PostgreSQL
默认配置会启动一个 PostgreSQL 容器,无需额外配置。
## 验证安装
1. 访问 http://localhost:8000/admin - Django 管理后台
2. 访问 http://localhost:8000/api/ - REST API
3. 访问 http://localhost:8000/graphql/?graphiql - GraphQL playground
4. 访问 http://localhost:3000 - React 前端
## 常见问题
### 迁移错误
如果遇到迁移错误,删除 migrations 文件并重新生成:
```bash
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
python manage.py makemigrations
python manage.py migrate
```
### 静态文件收集错误
```bash
python manage.py collectstatic --noinput
```
### 数据库连接错误
检查 `.env` 文件中的数据库配置是否正确。