request.GET的用法:
1.在页面上输入:http://127.0.0.1:8000/index/

print(request.GET,type(request.GET))
<QueryDict: {}> <class 'django.http.request.QueryDict'>
在收到GET请求后,Django会将后面的参数以字典的形式接收。如果无参数就显示空字典
2.在页面上输入:http://127.0.0.1:8000/index/?page=2
<QueryDict: {'page': ['2']}> <class 'django.http.request.QueryDict'>
3.request.GET["name"]="jerd" #直接进行添加操作,操作失败
查看源码:
from django.http.request import QueryDict
  class QueryDict(MultiValueDict):
        _mutable = True
        _encoding = None
        def __init__(self, query_string=None, mutable=False, encoding=None):pass
    在调用时,mutable=False,因此要向对request.GET接收的值及逆行更改。需要手动设置 _mutable = True
request.GET._mutable = True
request.GET["name"] = "jerd"
print(request.GET, type(request.GET))
<QueryDict: {'page': ['2'], 'name': ['jerd']}>
4.urlencode 将request.GET中的值拼接到一起,组成新的字符串
from django.http.request import QueryDict
request.GET._mutable = True
request.GET["name"] = "jerd"
print(request.GET.urlencode())  #page=2&name=jerd  类型为字符串
5.deepcopy(request.GET)
在对GET来的数据处理时,一般都另复制一份进行操作,使用deepcopy
import copy
params=copy.deepcopy(request.GET)
params["name"]="zhao"
print(params) #<QueryDict: {'name': ['zhao']}>
为什么能直接添加了呢?
在request.GET中mutable默认为False,需要设置为True才能添加数据,而在拷贝的request.GET中却可以
直接添加,原因是在拷贝之后,mutable值为True
查看其源码:即不管是深拷贝还是浅拷贝,mutable值均为True
 def __copy__(self):
        result = self.__class__('', mutable=True, encoding=self.encoding)
        pass

    def __deepcopy__(self, memo):
        result = self.__class__('', mutable=True, encoding=self.encoding)
        memo[id(self)] = result
        pass
 print("params的mutable值为:",params._mutable)  #params的mutable值为: True

2.ORM模型表知识补充

1.在models中创建模型表

class Book(models.Model):
    nid = models.AutoField(primary_key=True)
    title = models.CharField( max_length=32,verbose_name="书名")
    publishDate=models.DateField(verbose_name="出版日期")
    price=models.DecimalField(max_digits=5,decimal_places=2,verbose_name="价格")
    publish=models.ForeignKey(to="Publish",to_field="nid",on_delete=models.CASCADE,verbose_name="出版社")
    authors=models.ManyToManyField(to='Author',verbose_name="作者")
    def __str__(self):
        return self.title
View Code

相关文章: