mysql设置允许远程访问
sudo apt-get update
sudo apt-get install mysql-server
设置mysql允许远程访问,
首先编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf
注释掉bind-address = 127.0.0.1
重启mysql /etc/init.d/mysql restart
查看3306 netstat -an |grep 3306
mysql> Grant all privileges on . to ‘root’@’%’ identified by ‘password’ with grant option;
mysql> flush privileges;
创建数据库
create database name DEFAULT CHARSET utf8;
Django项目新建流程
- 下载django 和 xadmin
pip install -i https://pypi.mirrors.ustc.edu.cn/simple/ django==2.2
pip install git+git://github.com/sshwsfc/xadmin.git@django2
- 新建项目
django-admin startproject 项目名称
- 切换到项目目录下,启动app
python manage.py startapp 应用名app01
- 修改setting 配置文件
INSTALLED_APPS 增加app01的配置
INSTALLED_APPS = [
'ops.apps.OpsConfig',
'xadmin',
'crispy_forms',
'reversion',
]
LANGUAGE_CODE = 'zh-Hans'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False
ALLOWED_HOSTS = ['*', ]
STATIC_URL = '/static/'
STATIC_ROOT=os.path.join(BASE_DIR,"/static/")
- 创建路由
from django.contrib import admin
from django.urls import path
import xadmin
xadmin.autodiscover()
# version模块自动注册需要版本控制的 Model
from xadmin.plugins import xversion
xversion.register_models()
urlpatterns = [
path('xadmin/', xadmin.site.urls),
]
- 创建templates文件夹(模板),并修改文件夹位置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
安装requirement.txt
pip install -r requirement.txt
mysqlclient 推荐安装新版本这个,而不使用pymysql
9. 配置mysql 数据库,settings文件中,修改sqllite为mysql
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'login',
'HOST': '192.168.15.161',
'PORT': 3306,
'USER': 'root',
'PASSWORD': '123456',
}
9. 创建静态库文件夹,并在setting添加静态文件的位置
```python
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
- 在models.py添加类
class Publisher(models.Model):
name = models.CharField(max_length=32)
- 创建迁移文件,并且往数据库做迁移
python manage.py makemigrations
python manage.py migrate
启动项目
python manage.py runserver # 127.0.0.1:8000
启动其他端口
python manage.py runserver 80
python manage.py runserver 0.0.0.0:80
创建管理员
python manage.py createsuperuser
拉取静态文件到本地
python manage.py collectstatic
站点配置 https://blog.youkuaiyun.com/bocai_xiaodaidai/article/details/94395604
xadmin 教程:https://www.cnblogs.com/derek1184405959/p/8590360.html
xadmin整合:https://www.jianshu.com/p/3a3afda82f72