Python Django setting

python django 配置

  1. python 3.x 版本安装mysql

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    [root@dev cmdb]# cat cmdb/settings.py
    DATABASES = {
    'default': {
    'ENGINE' : 'django.db.backends.mysql',
    'NAME' : 'cmdb',
    'USER' : 'cmdb',
    'PASSWORD' : 'cmdb123',
    'HOST' : '127.0.0.1',
    'PORT' : '3306',
    }
    }

    注意: python 3.x 不在使用MySQL-python包了,而是使用PyMySQL

    [root@dev cmdb]# pip install PyMySQL
    [root@dev cmdb]# cat cmdb/__init__.py
    import pymysql
    pymysql.install_as_MySQLdb()
  2. django 创建数据库表

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    [root@dev cmdb]# python manage.py migrate
    System check identified some issues:
    WARNINGS:
    ?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
    HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/1.11/ref/databases/#mysql-sql-mode

    解决方法:
    [root@dev cmdb]# vim cmdb/settings.py
    DATABASES = {
    'default': {
    #'ENGINE': 'django.db.backends.sqlite3',
    #'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    'ENGINE' : 'django.db.backends.mysql',
    'NAME' : 'cmdb',
    'USER' : 'cmdb',
    'PASSWORD' : 'cmdb123',
    'HOST' : '127.0.0.1',
    'PORT' : '3306',
    'OPTIONS' : {
    'init_command' : 'SET default_storage_engine=INNODB',
    'init_command' : "SET sql_mode='STRICT_TRANS_TABLES'",
    'charset' : 'utf8',
    }
    }
    }
  3. 创建 model(就是创建数据库表)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    //创建数据库表
    [root@dev cmdb]# cat app/models.py
    from django.db import models
    # Create your models here.
    class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')

    class Choice(models.Model):
    question = models.ForeignKey(Question,on_delete=models.CASCADE)
    choice_text = models.CharField(max_length=200)
    votes = models.IntegerField(default=0)

    //将数据库表加载到应用中
    (oeaoo) [root@dev cmdb]# cat app/apps.py
    from django.apps import AppConfig
    class AppConfig(AppConfig):
    name = 'app'

    [root@dev cmdb]# vim cmdb/settings.py
    INSTALLED_APPS = [
    'app.apps.AppConfig',
    ]

    //生成sql配置文件
    [root@dev cmdb]# python manage.py makemigrations app
    [root@dev cmdb]# ls -1 app/migrations/
    0001_initial.py
    __init__.py
    __pycache__

    //检查sql语句
    [root@dev cmdb]# python manage.py sqlmigrate app 0001
    BEGIN;
    --
    -- Create model Choice
    --
    CREATE TABLE `app_choice` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `choice_text` varchar(200) NOT NULL, `votes` integer NOT NULL);
    --
    -- Create model Question
    --
    CREATE TABLE `app_question` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `question_text` varchar(200) NOT NULL, `pub_date` datetime NOT NULL);
    --
    -- Add field question to choice
    --
    ALTER TABLE `app_choice` ADD COLUMN `question_id` integer NOT NULL;
    ALTER TABLE `app_choice` ADD CONSTRAINT `app_choice_question_id_614eef1f_fk_app_question_id` FOREIGN KEY (`question_id`) REFERENCES `app_question` (`id`);
    COMMIT;

    //检查执行sql过程
    [root@dev cmdb]# python manage.py check
    System check identified no issues (0 silenced).

    //执行sql语句(创建数据库表)
    [root@dev cmdb]# python manage.py migrate
  4. Django shell环境

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    [root@dev cmdb]# python manage.py shell
    >>> from polls.models import Question,Choice
    >>> Question.objects.all()
    <QuerySet []>
    >>>
    >>> from django.utils import timezone
    >>> q = Question(question_text="What's new?",pub_date=timezone.now())
    >>> q.save()
    >>> q.id
    1
    >>> q.question_text
    "What's new?"
    >>> q.pub_date
    datetime.datetime(2017, 7, 11, 1, 44, 48, 472871, tzinfo=<UTC>)
    >>>
    >>> q.question_text="What's up?"
    >>> q.save()
    >>> Question.objects.all()
    <QuerySet [<Question: Question object>]>
  5. Django admin

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    //创建 Django user
    [root@dev cmdb]# python manage.py createsuperuser
    Username (leave blank to use 'root'): admin
    Email address: liu@xxx.cn
    Password:
    Password (again):
    This password is too short. It must contain at least 8 characters.
    This password is too common.
    Password:
    Password (again):
    Superuser created successfully.

    //启动服务
    [root@dev cmdb]# python manage.py runserver 0.0.0.0:8080

    //通过浏览器访问
    输入: http://192.168.13.208:8080/admin/
    输入账号和密码: admin/adminadmin

django.admin.login

1
2
3
4
5
6
//将应用app通过admin来管理
[root@dev cmdb]# vim app/admin.py
from django.contrib import admin
# Register your models here.
from .models import Question
admin.site.register(Question)

django.admin.manage

1
单击"Question",你可以对插入到数据库里面的数据进行操作(增删改除)

  1. Django view