Django 创建app 应用,数据库配置

时间:2021-02-02 17:49:00

一.create project
mkdir jango
cd jango 目录创建project myapp
django-admin startproject myapp
2.在给project创建app
cd myapp 的project下:
python manage.py startapp appname 创建一个app
python manage.py run server localhost:9999 启动jango server 默认:http://localhost:9999访问显示django页面表示创建app ok

  

二.数据库mysql以及可能遇到问题:
tips:
修改settings.py :
1.ALLOWED_HOSTS = [‘*‘,] 默认是空,如果想实现非localhost地址连接django server,
注意在同一网落,此外注意结尾要保留逗号","
DATABASES修改如下,其中NAME表示数据库你想要新建初始化数据的db名称,如果数据库已经存在此db,
先登录mysql所安装机器mysql -u root -p 然后drop database existedDbName;此外修改默认的数据库ENGINE sqlite3为mysql,再执行创建进行初始化迁移的db ,
 create database mysitedb CHARACTER SET utf8 COLLATE utf8_general_ci;:
DATABASES = {
    ‘default‘: {
        ‘ENGINE‘: ‘django.db.backends.mysql‘,
        ‘NAME‘: "mysitedb",
        ‘USER‘: ‘root‘,
        ‘PASSWORD‘: ‘root‘,
        ‘HOST‘: ‘192.168.110.128‘,
        ‘PORT‘: ‘3306‘,
    }
}
在进行数据库迁移前必须确保数据库已存在并且可以连接:
在project目录之前是myapp目录(settings.py所在的目录下)的init.py 添加:
import pymysql
pymysql.install_as_MySQLdb()
最后修改django默认语言和时区为Asia/Shanghai如下默认是:{ #LANGUAGE_CODE = ‘en-us‘
#TIME_ZONE = ‘UTC‘ }修改为:
LANGUAGE_CODE = ‘zh-hans‘
TIME_ZONE = ‘Asia/Shanghai‘
最后执行数据库初始化迁移migrate:
python manage.py  makemigrations
python manage.py migrate
执行迁移最终成功如下,也可以进入数据库检查mysitedb 也就是你创建的db:use dbname; show tables;:
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying sessions.0001_initial... OK
 检查数据库会如下:
 mysql> show tables;
 ---------------------------- 
| Tables_in_mysitedb         |
 ---------------------------- 
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
 ---------------------------- 
10 rows in set (0.05 sec)
这就是意味着数据库初始化迁移成功啦!!!
  数据库迁移过程可能遇到问题以及解决方案:
1.mysql 连接失败django.db.utils.InternalError:
(1130, "Host ‘192.168.110.1‘ is not allowed to connect to this MySQL server"):
    解决方案:
    1.1授权 settings.py 配置数据库用户root任意地址访问:
       use mysql ;
        update user set host = ‘%‘ where user =‘root‘;
        flush  privileges;
2.FAQ-RuntimeError: cryptography is required for sha256_password or caching_sha2_password
    解决:pip install cryptography

python version: 3.7.2
3.django.core.exceptions.ImproperlyConfigured:
mysqlclient 1.3.13 or newer is required; you have 0....
解决:cd C:Python37Libsite-packagesdjangodbbackendsmysql
    3.1修改base.py 和operations.py :

    3.2修改operations.py :
        query = query.decode(errors=‘replace‘)的decode修改为encode
    注释base.py :
    if version < (1, 3, 13):
    raise ImproperlyConfigured(‘mysqlclient 1.3.13 or newer is required; you have %s.‘ % Database.__version__)

三.settings.py管理应用多个:
INSTALLED_APPS = [
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
"app",
"app2",
"app3"
]