Django ---- (four)

Library management system

Press book authors

Press management

Show

# 查询所有的数据
all_publishers = models.Publisher.objects.all()   #  对象列表

render(request,'publisher_list.html',{'all_publishers':all_publishers})

Template syntax:

{{All_publishers}} variable

​ {% for i in all_publishers %}

​ {{ forloop.counter }} {{ i }}

​ {% endfor %}

New

# 方式一
ret = models.Publisher.objects.create(name=pub_name)
# 方式二
obj = models.Publisher(name=pub_name)
obj.save()

delete

pk = request.GET.get('pk')
query = models.Publisher.objects.filter(pk=pk)  # 对象列表
query.delete()  # 通过queryset 删除
query[0].delete()  # 通过单独的对象 删除

edit

obj = models.Publisher.objects.filter(pk=pk).first()  # 对象列表中第一个对象
obj.name = pub_name  # 内存中修改
obj.save()          # 提交
  1. django command

    1. download

      pip install django==1.11.23 - i

    2. Create a project

      django-admin startproject project name

    3. Startup project

      Cd to the root directory of the project

      python manage.py runserver # 127.0.0.1:8000

      python manage.py runserver 80 # 127.0.0.1:80

      python manage.py runserver 0.0.0.0:80 # 0.0.0.0:80

    4. Creating app

      python manage.py startapp app name

      registered

    5. Database Migration

      python manage.py makemigrations # create models under APP registered migration file detection

      python manage.py migrate # migration will change records to the database synchronization models

  2. django configuration settings

    INSTALLED_APPS = [

    "App01,

    ​ 'app01.apps.App01Config'

    ]

    DATABASES

    ENGINE: Engine

    NAME: The name of the database

    ​ HOST : IP

    ​ PORT : 3306

    USER: User Name

    PASSWORD: Password

    Static files

    ​ STATIC_URL = '/static/'

    ​ STATICFILES_DIRS=[

    ​ os.path.join(BASE_DIR,‘static’)

    ]

    Middleware

    Comment out csrf submit POST

    Templates TEMPLATES

     DIRS =[     os.path.join(BASE_DIR,‘templates’)]  
  3. mysql database using django process

    1. Create a mysql database

    2. settings in the configuration database

      ENGINE: Engine

      NAME: The name of the database

      ​ HOST : IP

      ​ PORT : 3306

      USER: User Name

      PASSWORD: Password

    3. Tell django modules are connected using pymysql mysql database;

      In the same directory with the settings of __init__.pywrite

      import pymysql

      pymysql.install_as_MySQLdb()

    4. Write in class under the app in models.py

      class Publisher(models.Model):
          name = models.Charfield(max_length=32)   # varcher(32)
    5. The migration command:

      python manage.py makemigrations # create models under APP registered migration file detection

      python manage.py migrate # migration will change records to the database synchronization models

  4. get and post the difference

    get

    Send get requests ways:

    1. form forms do not specify the method
    2. Enter directly enter the address in the address bar
    3. a title

    ?k1=v1&k2=v2 request.GET request.GET.get('k1')

    post

    form form method = 'post'

    request.POST request.POST.get('k1')

  5. snake

    Directed to an object and a mapping of relational database

    Map:

    Class - "Table

    Object - "records (rows)

    Properties - "field

    from app import models

    Inquire:

    models.Publisher.objects.get (name = 'xxxxx') # object has a query and a plurality of unique or almost on the error

    models.Publisher.objects.filter (name = 'xxxxx') # query all objects in the object list to meet the conditions queryset

    models.Publisher.objects.all () # query all data objects list queryset

    Add:

    models.Publisher.objects.create (name = 'xxxxx') # return value is the new object

    ​ obj = models.Publisher(name='xxxxx') obj.save()

    delete:

    ​ models.Publisher.objects.filter(pk=1) .delete()

    ​ obj = models.Publisher.objects.filter(pk=1) .first() obj.delete()

    modify:

    ​ obj = models.Publisher.objects.filter(pk=1) .first()

    ​ obj.name = 'xxxxx'

    ​ obj.save()

  6. The syntax of the template

    render (request, 'the template file name', { 'k1': v1})

    variable

    {K1} {}

    for loop

    {% for i in k1 %}

    ​ {{ forloop.counter }}

    ​ {{ i }}

    {% endfor %}

    Foreign key

    Many

    Library management system

    Press

    book

    Foreign key design

    class Book(models.Model):
        title = models.CharField(max_length=32)
        pid = models.ForeignKey('Publisher', on_delete=models.CASCADE) # 外键 
        #  on_delete  2.0 必填 
    

Inquire:

all_books = models.Book.objects.all()
print(all_books)
for book in all_books:
    print(book)
    print(book.pk)
    print(book.title)
    print(book.pub,type(book.pub))  # 所关联的对象 
    print(book.pub_id,type(book.pub_id))  # 所关联的对象的pk
    print('*' * 32)

New

models.Book.objects.create(title=title,pub=models.Publisher.objects.get(pk=pub_id))
models.Book.objects.create(title=title, pub_id=pub_id)

delete:

pk = request.GET.get('pk')
models.Book.objects.filter(pk=pk).delete()

edit:

book_obj.title = title
book_obj.pub_id = pub_id
# book_obj.pub = models.Publisher.objects.get(pk=pub_id)
book_obj.save()

Guess you like

Origin www.cnblogs.com/hql1117/p/11420386.html