当前位置:网站首页>debian10 install djando
debian10 install djando
2022-07-30 09:54:00 【xuwq2021】
debian10安装djando
一、基础配置
配置阿里云源
cat > /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb http://mirrors.aliyun.com/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
EOF
更新源:
apt update
安装常用工具
apt install -y vim git procps net-tools wget lrzsz
安装ssh服务
apt install -y openssh-server openssh-client
安装python3.9.1
echo "deb-src http://archive.debian.com/debian/ bionic main" >> /etc/apt/sources.list
#安装依赖
apt-get build-dep -y python3.5
cd /root
wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tar.xz
tar -xvf Python-3.9.1.tar.xz
cd Python-3.9.1
./configure --enable-optimizations
make -j 2
make install
ln -s /usr/local/bin/pip3.9 /usr/local/bin/pip
ln -s /usr/local/bin/python3.9 /usr/local/bin/python
#更新pip
/usr/local/bin/python3.9 -m pip install --upgrade pip
#修改pipThe source is the Alibaba Cloud software source
mkdir ~/.pip
cat > ~/.pip/pip.conf << EOF
[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
EOF
二、安装Django并创建项目
安装Django
python -m pip install Django
查看版本
python -m django --version
创建Django项目
Enter the directory where you want to store the project code and execute the following command,创建名为zmtdjango的项目:
django-admin startproject zmtdjango
After executing the command, a file named zmtdjango的目录,目录结构如下:
zmtdjango/
|-- manage.py
`-- zmtdjango
|-- __init__.py
|-- settings.py
|-- urls.py
`-- wsgi.py
三、配置Django
1、配置mysql数据库
安装库文件
安装pymysql和cryptography:
pip install pymysql
pip install cryptography
修改settings.py文件
打开项目下的settings.py文件,并对“DATABASES”部分进行编辑:
原文如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
修改为:
#DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
#}
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'zmtmanage',
'USER': 'zmtmanage',
'PASSWORD': 'zmtmanage=123',
'HOST': 'mysql',
'POST': 3306,
'OPTIONS': {
'charset': 'utf8mb4'},
}
}
在settigns.py文件中修改
修改__init__.py文件
Add the following code to the filezmtdjango/zmtdjango/___init__.py中:
import pymysql
pymysql.version_info = (1, 4, 13, "final", 0)
pymysql.install_as_MySQLdb()
迁移数据库
Execute the following command to migrate the database(创建Django数据库表):
python manage.py migrate
2、Create a background management user
python manage.py createsuperuser
3、修改语言
LANGUAGE_CODE = 'zh-hans'
在settigns.py文件中修改
4、修改时区
TIME_ZONE = 'Asia/Shanghai'
在settigns.py文件中修改
5、设置允许访问Django的方式
# All access is allowed
ALLOWED_HOSTS = ['*']
# 只允许列表中的ip地址访问
ALLOWED_HOSTS = ['192.168.1.2', '192.168.1.3']
# 只允许通过域名访问,禁止通过IP访问
ALLOWED_HOSTS = ['test.com', 'www.test.com', '*.test.cn']
在settigns.py文件中修改
四、创建应用
创建应用
python manage.py startapp web
web:应用名称
注册应用
修改settings.py文件内的INSTALLED_APPS选项,Add the created app to it:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'importfiles',
]
在settigns.py文件中修改
五、迁移数据库
This step is after creating the app,The method you want to use when creating or modifying table structures within the database for your application.
生成迁移文件
python manage.py makemigrations
执行迁移文件
#Defaults to all modifications under the projectmodes文件进行迁移
python manage.py migrate
#对某个appModified belowmodel文件进行迁移
python manage.py migrate app_name
#to a migration file(0004_xxx.py):
python manage.py migrate app_name 0004
六、报错信息
报错1
报错信息:
Invalid HTTP_HOST header: ‘xxx.com’. You may need to add u’xxx.com’ to ALLOWED_HOSTS
解决方法:
修改settings.py文件内的ALLOWED_HOSTS,原内容:
ALLOWED_HOSTS = []
修改后的内容:
ALLOWED_HOSTS = ['*']
报错2
报错信息:
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.0 or newer is required; you have 0.10.1
解决方法:
修改__init__.py文件内容如下:
import pymysql
pymysql.version_info = (1, 4, 13, "final", 0)
pymysql.install_as_MySQLdb()
报错3
报错信息:
RuntimeError: cryptography is required for sha256_password or caching_sha2_password
解决方法:
执行如下命令,安装cryptography
pip install cryptography
七、参考资料
[django报错解决:Invalid HTTP_HOST header: ‘xxx.com’. You may need to add u’xxx.com’ to ALLOWED_HOSTS.](https://www.cnblogs.com/zqifa/p/django-setting-1.html)
RuntimeError: cryptography is required for sha256_password or caching_sha2_password
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.0 or newer is required
边栏推荐
- hcip06 ospf特殊区域综合实验
- 虚幻引擎图文笔记:could not be compiled. Try rebuilding from source manually.问题的解决
- Unreal Engine Graphic Notes: could not be compiled. Try rebuilding from source manually. Problem solving
- leetcode 剑指 Offer 10- II. 青蛙跳台阶问题
- 北京突然宣布,元宇宙重大消息
- ospf2双点双向重发布(题2)
- Unity性能分析 Unity Profile性能分析工具
- leetcode 剑指 Offer 25. 合并两个排序的链表
- Test automation selenium (a)
- HR团队如何提升效率?人力资源RPA给你答案
猜你喜欢

【HMS core】【FAQ】HMS Toolkit典型问题合集1

leetcode 剑指 Offer 25. 合并两个排序的链表

使用 Neuron 接入 Modbus TCP 及 Modbus RTU 协议设备

20220728 Use the bluetooth on the computer and the bluetooth module HC-05 of Huicheng Technology to pair the bluetooth serial port transmission

快解析结合任我行crm

(***重点***)Flink常见内存问题及调优指南(一)

聊聊 MySQL 事务二阶段提交

一文理解分布式开发中的服务治理

水电表预付费系统

怎么在本地电脑上运行dist文件
随机推荐
MySQL中使用IN 不会走索引分析以及解决办法
初识Apifox——如何使用Apifox做一个简单的接口测试
els 方块停在方块上。
conda 导出/导出配置好的虚拟环境
Excel xlsx file not supported两种解决办法【杭州多测师】【杭州多测师_王sir】
团队级敏捷真的没你想的那么简单
HCIP --- MPLS VPN实验
元宇宙改变人类工作模式的四种方式
STM8L_库函数-模板搭建
连接mysql报错WARN: Establishing SSL connection without server‘s identity verification is not recommended
The sword refers to offer 48: the longest non-repeating substring
一个近乎完美的 Unity 全平台热更方案
图像分析:投影曲线的波峰查找
国外资源加速下载器,代码全部开源
MySQL Explain 使用及参数详解
CVTE校招笔试题+知识点总结
Apache DolphinScheduler新一代分布式工作流任务调度平台实战-上
编译报错: undefined reference to `google::FlagRegisterer::FlagRegisterer解决方法
606. Create a string from a binary tree (video explanation!!!)
读书笔记:《这才是心理学:看穿伪心理学的本质(第10版)》