1. 当前位置:网站首页 > Python环境

Django项目初始化


Ubuntu20.04 Django项目初始化

安装基本环境

# 安装python3.8环境
sudo apt update
sudo apt-get upgrade
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt install python3.8
# 安装nginx
sudo apt install nginx
sudo systemctl status nginx
所有的 Nginx 配置文件都在/etc/nginx/目录下。
主要的 Nginx 配置文件是/etc/nginx/nginx.conf。
页面文件在 /var/www/html
# 安装msyql
sudo apt install mysql-server
sudo systemctl status mysql
# 修改密码
sudo mysql
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password by 'Liu123456!';
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password by 'Pg123456!';
mysql> flush privileges;
mysql> exit;
sudo mysql_secure_installation
# 修改root密码
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
# 删除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
# 不允许root远程登陆
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
# 移除test数据库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
# 重新载入权限表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

vim /etc/mysql/mysql.conf.d/mysqld.cnf
注释 bind-address            = 127.0.0.1
sudo /etc/init.d/mysql restart
# 通过键入以下命令验证安装是否成功:
python3 --version
# 安装diango
pip install Django==3.2.8 -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
sudo apt-get install libmysqlclient-dev gcc python3.8-dev
sudo apt-get install python3-django
pip install mysqlclient -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
# 创建项目
django-admin startproject jw
# 启动项目
python3 manage.py runserver 0.0.0.0:8000

基本配置

设置允许网段访问

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# 修改 [] 为 ['*']
ALLOWED_HOSTS = ['*']

设置时区

# Internationalization
# 修改 LANGUAGE_CODE = 'en-us' 为 LANGUAGE_CODE = 'zh-Hans'
LANGUAGE_CODE = 'zh-Hans'
# 修改 TIME_ZONE = 'UTC' 为 TIME_ZONE = 'Asia/Shanghai'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = True

将数据库从sqlite 修改为 mysql

# Database
# 修改前
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Database
# 修改后
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': 3306,
'USER': 'root',
'PASSWORD': 'liu123456',
'NAME': 'test'
}
}

执行 python3 manage.py migrate 开始导入基础表到mysql


本文最后更新于2022-4-2,已超过 3个月没有更新,如果文章内容或图片资源失效,请留言反馈,我们会及时处理,谢谢!
版权说明

本文地址:http://www.liuyangdeboke.cn/?post=23
未标注转载均为本站远程,转载请注明文章出处:

发表评论

联系我们

在线咨询:点击这里给我发消息

微信号:17721538135

工作日:9:00-23:00,节假日休息

扫码关注