django学习:图书管理系统的增删改查,choices参数,MTV与MVC模型,多对多关系的三种创建方式,Ajax

图书管理系统的增删改查

from django.shortcuts import render,redirect,HttpResponse
from app01 import models
# Create your views here.
def home(request):
    return render(request,'home.html')

def book_list(request):
    #先查询出所有的书籍信息,传递给html页面
    book_queryset = models.Book.objects.all()  #前后端交互
    return render(request,'book_list.html',locals())


def book_add(request):
    if request.method=='POST':
        #获取前端提交过来的数据
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_date = request.POST.get('publish_date')
        publish_id = request.POST.get('publish')
        author_list = request.POST.getlist('author')
        #操作数据库存储数据
        #书籍表
        book_obj = models.Book.objects.create(title=title,price=price,publish_date=publish_date,publish_id=publish_id)
        #书籍与作者关系表
        book_obj.author.add(*author_list)
        return redirect('book_list')

    #先获取当前系统中所有的出版社信息和作者信息
    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()
    return render(request,'book_add.html',locals())


def book_edit(request,edit_id):
    #获取当前用户想要编辑的书籍对象,展示到页面
    edit_obj = models.Book.objects.filter(pk=edit_id).first()
    if request.method=='POST':
        #获取前端提交过来的数据
        title = request.POST.get('title')
        price = request.POST.get('price')
        publish_date = request.POST.get('publish_date')
        publish_id = request.POST.get('publish')
        author_list = request.POST.getlist('author')
        models.Book.objects.filter(pk=edit_id).update(title=title,
                                                      price=price,
                                                      publish_date=publish_date,
                                                      publish_id=publish_id,
                                                      )
        #改第三张关系表
        edit_obj.author.set(author_list)
        return redirect('book_list')
    #获取当前用户想要编辑的书籍对象,展示到页面
    edit_obj = models.Book.objects.filter(pk=edit_id).first()
    publish_queryset = models.Publish.objects.all()
    author_queryset = models.Author.objects.all()
    return render(request,'book_edit.html',locals())


def book_delete(request,delete_id):
    models.Book.objects.filter(pk=delete_id).delete()
    return redirect('book_list')

choices参数(数据库字段设计常见)

'''
只要某一个字段的可能性是可以列举完全的,那么我们一般使用choices参数,使用广泛
'''
class User(models.Model):
    username = models.CharField(max_length=32)
    age = models.IntegerField()
    #性别
    gender_choice = (
        (1,'男'),
        (2,'女'),
        (3,'其他'),
    )
    gender = models.IntegerField(choices=gender_choice)

    score_choices = (
        ('A','优秀'),
        ('B','良好'),
        ('C','及格'),
        ('D','不合格'),
    )
    score = models.CharField(choices=score_choices)
    
    
    
    
    import django
    django.setup()
    from app01 import models
    # models.User.objects.create(username='lz',age=18,gender=1)
    # models.User.objects.create(username='lz1',age=28,gender=2)
    # models.User.objects.create(username='lz2',age=38,gender=3)
    # models.User.objects.create(username='lz3',age=48,gender=4)
    # models.User.objects.create(username='lz4',age=58,gender=2)


    #取
    user_obj = models.User.objects.filter(pk=1).first()
    #只要是choices参数的字段,想要获取对应的信息,固定写法get_字段名_display()
    print(user_obj.get_gender_display())

MTV与MVC模型

#MTV:django号称是MTV模型
M:models
T:templates
V:views
    
#MVC:其实django本质也是MVC
M:models
V:views
C:controller

多对多关系的三种创建方式

#全自动:利用orm自动创建第三张关系表
class Book(models.Model):
    name = models.CharField(max_length=32)
    authors = models.ManyToManyField(to='Author')
class Author(models.Model):
    name = models.CharField(max_length=32)
    """
    优点:代码不需要自己写,还支持orm提供操作第三张关系表的方法
    缺点:第三张关系表的扩展性差(不能添加字段)
    """
#纯手动
class Book(models.Model):
    name = models.CharField(max_length=32)
class Author(models.Model):
    name = models.CharField(max_length=32)    
class Book2Author(models.Model):
    book_id = models.ForeignKey(to='Book')
    author_id = models.ForeignKey(to='Author')
    """
    优点:可以扩展
    缺点:写的代码较多,不能使用orm提供的简单方法(add,remove)
    """
#半自动
class Book(models.Model):
    name = models.CharField(max_length=32)
    authors = models.ManyToManyField(to='Author',
                                     through='Book2Author',
                                     through_fields=('book','author')
                                     )
class Author(models.Model):
    name = models.CharField(max_length=32)

class Book2Author(models.Model):
    book = models.ForeignKey(to='Book')
    author = models.ForeignKey(to='Author')
    """
    可以使用orm的正反向查询,但是不能使用add,set,remove,clear这四个方法
    """

Ajax

 '''
异步提交
局部刷新
例子:GitHub注册
	动态获取用户名实时的跟后端确认并实时展示的前端
	
朝后端发送请求的方式
	1、浏览器地址直接输入url回车          GET请求
	2、a标签href属性						GET请求
	3、form表单						GET请求/POST请求
	4、Ajax							GET请求/POST请求
	
Ajax最大的优点是不重新加载整个页面的情况下,可以与服务器交换数据并更新部分网页内容
'''

你可能感兴趣的:(django,python,学习)