django 项目基本配置

项目工程初始化

安装框架

go 复制代码
pip install django

使用命令创建项目

python 复制代码
django-admin startproject 项目名称

效果

根目录创建apps用以放置所有包

切换至apps目录创建子应用

python 复制代码
python ../manage.py startapp user

muxi_shop_back/settings.py

python 复制代码
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# 把apps加入到项目根的搜索路径,导入的时候可以直接从源码包进行导入
sys.path.insert(0,BASE_DIR)
sys.path.insert(0,os.path.join(BASE_DIR,"apps"))

数据库配置

创建数据库

配置静态文件路径

python 复制代码
# 图片访问路径 http://localhost:8080/static/product_images/232454.jpg
STATIC_URL = "static/"
# 配置一下文件所在路径
STATICFILES_DIT=[
    os.path.join(BASE_DIR,'static')
]

数据库配置

如果是协同开发数据库表是提前准备好的,可以将表的字段映射到本地

python 复制代码
python manage.py inspectdb --database default goods > apps/goods/models.py

往该模型类追加
python manage.py inspectdb --database default goods >> apps/goods/models.py

安装DRF框架

python 复制代码
pip install djangorestframework

setting配置文件

python 复制代码
INSTALLED_APPS = [
    "rest_framework"
]

注: 安装mysqlclient 可能会显示以下错误

下载对应python解释器的版本

python 复制代码
https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient

切换到下载文件目录

python 复制代码
pip install mysqlclient-1.4.6-cp38-cp38-win_amd64.whl
相关推荐
宋小黑8 分钟前
JDK 6到25 全版本网盘合集 (Windows + Mac + Linux)
java·后端
花酒锄作田12 分钟前
SQLAlchemy中使用UPSERT
python·sqlalchemy
SoleMotive.13 分钟前
一个准程序员的健身日志:用算法调试我的增肌计划
python·程序员·健身·职业转型
念何架构之路17 分钟前
Go进阶之panic
开发语言·后端·golang
先跑起来再说20 分钟前
Git 入门到实战:一篇搞懂安装、命令、远程仓库与 IDEA 集成
ide·git·后端·elasticsearch·golang·intellij-idea
亓才孓21 分钟前
[Properties]写配置文件前,必须初始化Properties(引用变量没执行有效对象,调用方法会报空指针错误)
开发语言·python
Bruk.Liu26 分钟前
(LangChain 实战14):基于 ChatMessageHistory 自定义实现对话记忆功能
人工智能·python·langchain·agent
大江东去浪淘尽千古风流人物41 分钟前
【VLN】VLN(Vision-and-Language Navigation视觉语言导航)算法本质,范式难点及解决方向(1)
人工智能·python·算法
Swift社区42 分钟前
Gunicorn 与 Uvicorn 部署 Python 后端详解
开发语言·python·gunicorn
Coinsheep1 小时前
SSTI-flask靶场搭建及通关
python·flask·ssti