# wdadmin-django **Repository Path**: wd-admin/wdadmin-django ## Basic Information - **Project Name**: wdadmin-django - **Description**: 基于 Django 实现的后端 - **Primary Language**: Python - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-07-24 - **Last Updated**: 2025-12-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # WdAdmin-Django WdAdmin后台管理系统——Django后端。 ## 平台简介 WDAdmin 后台管理系统的后端是基于 Python 开发,前端基于 Vue 开发,借鉴了优秀的开源项目 [dvadmin3](https://django-vue-admin.com/), 旨在打造开箱即用的后台管理系统,不同的分支会对应不同的功能,总有一款刚好适合你的需求。 简单有简单的好处,如果业务明确不需要一些功能,上全套功能的系统反而会因为繁琐的配置而导致各种问题,学习成本较大。 因此请根据需要选择合适的分支。 ### 通用功能 - 用户管理 - 字典管理 ### 分支t1 — 前端静态路由 t1 分支为最基础版本,拥有简单的前端权限控制,其中用户角色用来管理菜单权限,用户按钮权限用来控制前端的增删改等按钮权限(禁用/启用)。 - 用户角色通过前端自动生成的静态路由来配置,也就是说,通过在前端路由中设置角色,然后给用户设置角色,以此控制用户是否拥有此菜单。 - 用户按钮权限只作用于前端,后端暂时没有进行验证。如果需要,可以自己开发 DRF 的自定义权限类。 ### 分支t2 — 后端动态路由 t2 分支为基于角色的权限控制版本,路由菜单通过后端动态返回。给用户分配角色,给角色分配路由菜单。 ## 开始 建议使用虚拟环境,本项目使用 Pipenv 管理虚拟环境包,你也可以使用其它的管理工具。 1. 安装 pipenv ``` pip install pipenv ``` 2. 创建虚拟环境:进入到项目目录下,创建虚拟环境并进入到虚拟环境 ``` pipenv shell ``` 3. 安装项目依赖包 ``` pipenv install ``` 4. 配置环境变量 本项目分为开发(dev)、测试(test)和线上(online)三个环境,你需要配置环境变量 `WDADMIN_ENV`的值为相应的环境。 在不同的环境读取不同的配置,这样测试和上线就不需要手动改配置。参考 `wdadmin.config.__init__.py` 配置文件。 5. 生成数据库迁移文件 ``` python manage.py makemigrations ``` 请确保你的应用包含 `migrations/__init__.py` 文件,否则可能需要指定应用名称才会生成迁移文件。 6. 迁移数据库 ``` python manage.py migrate ``` 7. 初始化系统 ``` python manage.py init ``` 8. 运行 ``` python manage.py runserver 0.0.0.0:8000 ``` 如果你使用 Pycharm 专业版,右上角有按钮可以直接运行 Django 项目。 浏览器访问 http://127.0.0.1:8000/docs 查看API文档。 ## 国际化 参考文档:https://docs.djangoproject.com/zh-hans/5.2/topics/i18n/translation/#localization-how-to-create-language-files 1. 进入到应用目录下,创建一个 `locale` 文件夹 2. 创建语言文件,在当前目录下执行命令: ```shell django-admin makemessages -l en -e py ``` 这表示对 `py` 文件生成 `en` 语言文件(本项目为将中文转为英文)。 ## 部署 本项目使用 gunicorn 部署后端 Django 应用。以下采用 Linux 系统部署该项目,使用 systemctl 管理 gunicorn 服务。 1. 新增 systemctl 的 gunicorn 服务配置 ```shell vim /etc/systemd/system/gunicorn.service ``` 新增如下内容: ``` [Unit] Description=Gunicorn Server for WDAdmin After=network.targer [Service] User=root Group=root WorkingDirectory=/data/wdadmin-django ExecStart=pipenv run gunicorn --config=gunicorn_conf.py --env WDADMIN_ENV=online wdadmin.wsgi:application Restart=always Enviroment=WDADMIN_ENV=online [Install] WantedBy=multi-user.target ``` - WorkingDirectory:请配置为你的项目的路径。 - ExecStart:启动命令。 注意:`WDADMIN_ENV` 环境变量需要配置在 `ExecStart` 中。 2. 启动 gunicorn 服务 ```shell systemctl start gunicorn # 启动服务 systemctl stop gunicorn # 停止服务 systemctl restart gunicorn # 重启服务 systemctl status gunicorn # 查看服务状态 ``` 3. 配置代理(使用`OpenResty`或`Nginx`) 以下配置仅供参考,相关参数根据自身需求设置。 ``` http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; server_tokens off; upstream wdadmin_django { server 127.0.0.1:8000; keepalive 5; } server { listen 80; server_name _; # 后端API代理 location ^~ /api { proxy_http_version 1.1; proxy_set_header Connection ""; proxy_pass http://wdadmin_django; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } # 前端代理 location ^~ /wdadmin_web/ { alias /data/wdadmin-soybean/dist/; index index.html; try_files $uri $uri/ /wdadmin_web/index.html; } location / { deny all; } } } ```