Django front and rear ends data transfer, ajax, pager

Of Contents:

  A, MTV and the MVC pattern
  Second, many to many table three ways to create ways
  Third, the front and rear ends of data transmission
  Four, Ajax
  Fifth, the bulk insert data
  Six custom pager

A, MTV and the MVC pattern

  1. MTV model

    Django就是基于MTV模型的框架,其中:
    M:模型层 models.py T:templates V:视图层 views 
  2. MVC model

    M:模型层 models
    V:视图层 views
    C:控制器 controller 
    本质:django的MTV也是MVC
    

Second, many to many table three ways to create ways

  1. The first: Django ORM automatically help create

    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) # 多对多字段在任意一张表中都可以创建,一般建议在查询频率高的那张表中建 # 在Django中,上面的操作--指定Book与Author字段是多对多的关系,Django会自动创建二者关系的表 # 好处是不用操心第三行表的创建 # 不足的地方在于,无法在这张表中添加新的字段(只是本表的字段),只能是指定的这几个关联的字段 
  2. Second: Create a third table purely manual

    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 = models.ForeignKey(to='Book') author = models.ForeignKey(to='Author') info = models.CharField(max_length=32) # 手动新建第三张表,将该表中的几个字段关联到Book和Author表中 # 不足的地方在于不支持双下划线的反向查询方式 # 好处在于扩展性大,可以添加出来关联的字段,还可以添加这个表自己的字段 
  3. Third: to create a semi-automatic third table

    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) # book = models.ManyToManyField( to='Book',through='Book2Author',through_fields=('author','book')) class Book2Author(models.Model): book = models.ForeignKey(to='Book') author = models.ForeignKey(to='Author') info = models.CharField(max_length=32) # 同样是要手动创建第三张表,像第二种方法那样,同样也需要操作第一种的方式,不过要添加参数 # 这样建起来虽然麻烦了点,但是扩展性强的同事,又可以通过点点点(基于对象/双下划线)来操作数据 

Third, the front and rear ends of data transmission

  1. urlencoded

    Request Headers里:Content-Type: application/x-www-form-urlencoded
    
    前端对应的数据格式
    	name=tyft&password=23s3
        
    后端获取数据
    	request.POST
        
    ps: django会将urlencoded编码的数据解析自动放到request.POST 
  2. multipart/form-data

    form表单传输文件的编码格式
    后端获取文件格式数据
    	request.FILES
        
    后端获取普通键值对数据
    	request.POST
    
  3. application/json

    ajax发送json格式数据,数据在request.body需要注意的点
    	编码与数据格式要一致 

Four, Ajax

To link: https://www.cnblogs.com/xt12321/p/11025037.html

Fifth, the bulk import data

l = []
for i in range(10000):
	l.append(models.Book2(name='第%s本书'%i)) models.Book2.objects.bulk_create(l) # 批量插入数据 

Six custom pager

后端:
    book_list = models.Book2.objects.all()
    # 数据总条数
    all_count = book_list.count() # 当前页 current_page = request.GET.get('page',1) # 示例一个分页器对象 page_obj = my_page.Pagination(current_page=current_page,all_count=all_count) # 对总数据进行切片 page_queryset = book_list[page_obj.start:page_obj.end] 前端: {{ page_obj.page_html|safe }} # 帮你渲染的是带有bootstrap样式的分页器 # 直接导包用就好

给个单独的链接:https://www.cnblogs.com/xt12321/p/11025055.html

Guess you like

Origin www.cnblogs.com/xt12321/p/11024417.html