当前位置:网站首页>debian10安装djando
debian10安装djando
2022-07-30 09:12: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
#修改pip源为阿里云软件源
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项目
进入到要存放项目代码的目下执行如下命令,创建名为zmtdjango的项目:
django-admin startproject zmtdjango
执行完命令后会在当前目录下生成一个名为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文件
将如下代码添加到文件zmtdjango/zmtdjango/___init__.py中:
import pymysql
pymysql.version_info = (1, 4, 13, "final", 0)
pymysql.install_as_MySQLdb()
迁移数据库
执行以下命令迁移数据库(创建Django数据库表):
python manage.py migrate
2、创建后台管理用户
python manage.py createsuperuser
3、修改语言
LANGUAGE_CODE = 'zh-hans'
在settigns.py文件中修改
4、修改时区
TIME_ZONE = 'Asia/Shanghai'
在settigns.py文件中修改
5、设置允许访问Django的方式
# 允许所有的访问
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选项,将创建的应用加入其中:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'importfiles',
]
在settigns.py文件中修改
五、迁移数据库
此步骤是在创建应用后,想为应用在数据库内创建或修改表结构时使用的方法。
生成迁移文件
python manage.py makemigrations
执行迁移文件
#默认对项目下的所有修改过的modes文件进行迁移
python manage.py migrate
#对某个app下修改过的model文件进行迁移
python manage.py migrate app_name
#确到某个迁移文件(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
边栏推荐
- iperf3 参数选项详细说明
- Explain the problem of change exchange in simple terms - the shell of the backpack problem
- leetcode 剑指 Offer 58 - I. 翻转单词顺序
- PyQt5快速开发与实战 8.1 窗口风格
- leetcode 剑指 Offer 63. 股票的最大利润
- 0729放假自习
- 团队级敏捷真的没你想的那么简单
- HCIP --- MPLS VPN实验
- Functional Interfaces & Lambda Expressions - Simple Application Notes
- 积分专题笔记-积分的定义
猜你喜欢
随机推荐
[Yugong Series] July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
Test automation selenium (a)
Unified exception handling causes ResponseBodyAdvice to fail
一文理解分布式开发中的服务治理
获取显示器数据
An article to understand service governance in distributed development
虚幻引擎图文笔记:could not be compiled. Try rebuilding from source manually.问题的解决
Detailed description of iperf3 parameter options
快解析结合象过河erp
Google Cloud Spanner的实践经验
els 方块向左移动
HR团队如何提升效率?人力资源RPA给你答案
How to use Jmeter to carry out high concurrency in scenarios such as panic buying and seckill?
编译报错: undefined reference to `google::FlagRegisterer::FlagRegisterer解决方法
日志导致线程Block的这些坑,你不得不防
【云原生】Kubernetes入门详细讲解
功能测试、UI自动化测试(web自动化测试)、接口自动化测试
Oracle 创建和操作表
342 · Valley Sequence
conda 导出/导出配置好的虚拟环境