• Django 学习笔记


    官方文档

    https://www.djangoproject.com

    中文文档

    https://docs.djangoproject.com/zh-hans/2.0/

    django安装

    sudo /usr/local/python37/bin/pip install Django==1.8.2

    sudo /usr/local/python37/bin/pip install django-json-response --upgrade


    django-admin --version
    1.8.2
    需要注意pip的全路径,如果使用某个python版本目录的pip,则会将django安装到相应的python目录中;如果直接使用pip,则会将django安装到系统
    which django-admin
    /usr/bin/django-admin

    容器创建

    django-admin startproject mydevops

    应用创建

    cd mydevops/

    python manage.py startapp scanhosts

    将应用加入容器

    vim mydevops/settings.py

    INSTALLED_APPS = (
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'scanhosts',
    )

    数据库的详细配置可以参考

    https://docs.djangoproject.com/zh-hans/2.0/ref/databases/#mysql-notes

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'mydevops',
            'USER': 'admin',
            'PASSWORD': 'mysqlpass',
            'HOST':'127.0.0.1',
            'PORT':'3306',
            'OPTIONS': {},
            'init_command':'SET storage_engine=INNODB,'
                            'SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED, autocommit=1,names "utf8"'
        },
    }

    数据库创建库与用户
    create database mydevops;

    grant all privileges on *.* to admin@'127.0.0.1' identified by 'mysqlpass';

    flush privileges;

    model模型

    CharField        字符串类型

    IntegerFiled     整数类型

    DateTimeField 时间日期字段

    ForeignKey     定义多对一关系

    修改新创建应用的models.py文件

    
    
    from django.db import models
    
    class UserIPInfo(models.Model):
        ip = models.CharField(max_length=40, default='', verbose_name=u'ip address', null=True)
        time = models.DateTimeField(verbose_name=u"updated time", auto_now=True)
    
        class Meta:
            verbose_name = u'user access adress message table'
            verbose_name_plural = verbose_name
            db_table = "useripinfo"
    
    class BrowseInfo(models.Model):
        useragent = models.CharField(max_length=100, default='', verbose_name=u'use agent message',null=True)
        models.CharField(max_length=256, verbose_name=u"unique id", default="")
    
        userip = models.ForeignKey('UserIPInfo')
    
    
        class Meta:
            verbose_name = u'user access adress message table'
            verbose_name_plural = verbose_name
            db_table = "browseinfo"
    
    


    模块迁移

     生成迁移文件

    [root@at2 mydevops]# python manage.py makemigrations
    Migrations for 'scanhosts':
      0001_initial.py:
        - Create model BrowseInfo
        - Create model UserIPInfo
        - Add field userip to browseinfo
    [root@at2 mydevops]# cd scanhosts/migrations/
    [root@at2 migrations]# ls
    0001_initial.py  __init__.py  __init__.pyc

    生成库表,然后去数据库中验证

    [root@at2 mydevops]# python manage.py migrate

    mysql> desc browseinfo;
    +-----------+--------------+------+-----+---------+----------------+
    | Field     | Type         | Null | Key | Default | Extra          |
    +-----------+--------------+------+-----+---------+----------------+
    | id        | int(11)      | NO   | PRI | NULL    | auto_increment |
    | useragent | varchar(100) | YES  |     | NULL    |                |
    | userip_id | int(11)      | NO   | MUL | NULL    |                |
    +-----------+--------------+------+-----+---------+----------------+
    3 rows in set (0.00 sec)

    模块更新

    比如,添加一个字段,或者改变一个字段长度,首先改变models.py

    useragent = models.CharField(max_length=200, default='', verbose_name=u'use agent message',null=True)

    然后再次执行,即可完成变更

    python manage.py makemigrations

    python manage.py migrate

     django交互界面

    $ python manage.py shell
    >>> from scanhosts.models import UserIPInfo
    >>> for item in UserIPInfo.objects.all():
    ...     print(item.__dict__)
    ...
    {'_state': <django.db.models.base.ModelState object at 0x7f2d2690a908>, 'id': 1, 'ip': '127.0.0.1', 'time': datetime.datetime(2019, 6, 23, 7, 11, 30, 474131, tzinfo=<UTC>)}
    {'_state': <django.db.models.base.ModelState object at 0x7f2d2690aa58>, 'id': 2, 'ip': '192.168.81.131', 'time': datetime.datetime(2019, 6, 23, 7, 17, 30, 121449, tzinfo=<UTC>)}
    {'_state': <django.db.models.base.ModelState object at 0x7f2d2690aac8>, 'id': 3, 'ip': '192.168.81.1', 'time': datetime.datetime(2019, 6, 23, 7, 18, 6, 749511, tzinfo=<UTC>)}



  • 相关阅读:
    解决 Android SDK Manager不能下载旧版本的sdk的问题
    [置顶] 如何合并文件中的内容?
    JSTL解析——005——core标签库04
    C中的几组指针
    别动我的奶酪:CSV文件数据丢零现象及对策
    重载(overload),覆盖/重写(override),隐藏(hide)
    IOS 轻量级数据持久化 DataLite
    记录路径dp-4713-Permutation
    android 多媒体数据库详解
    Data Recovery Advisor(数据恢复顾问)
  • 原文地址:https://www.cnblogs.com/perfei/p/11071969.html
Copyright © 2020-2023  润新知