• modelform跨站请求


    csrf 跨站请求伪造

     1 ajax({
     2     
     3     data:{csrfmiddlewaretoken:$('[name=csrfmiddlewaretoken]').val(),
     4             // csrfmiddlewaretoken:'{{ csrf_token }}',
     5         }
     6 })
     7 ajax({
     8     
     9     url:'/test/',
    10     type:'post',
    11     headers:{
    12         "X-CSRFToken": $.cookie('csrftoken'),
    13     },
    14 })

    jquery操作cookie,文件地址http://plugins.jquery.com/cookie/

    https://www.cnblogs.com/clschao/articles/10480029.html

     1 <script src="{% static 'jquery.js' %}"></script>

    2 <script src="{% static 'jquery.cookie.js' %}"></script> 

    同源机制

     1 浏览器的一个安全机制,非同源不允许直接互相访问,同源:协议,域名(ip地址),端口号三项相同才是同源
     2 
     3 简单请求和复杂请求
     4 简单请求:
     5 (1) 请求方法是以下三种方法之一:(也就是说如果你的请求方法是什么put、delete等肯定是非简单请求)
     6     HEAD
     7     GET
     8     POST
     9 (2)HTTP的头信息不超出以下几种字段:(如果比这些请求头多,那么一定是非简单请求)
    10     Accept
    11     Accept-Language
    12     Content-Language
    13     Last-Event-ID
    14     Content-Type:只限于三个值application/x-www-form-urlencoded、multipart/form-data、text/plain,也就是说,如果你发送的application/json格式的数据,那么肯定是非简单请求,vue的axios默认的请求体信息格式是json的,ajax默认是urlencoded的。
    15 
    16 简单请求只发送一次请求
    17 
    18 
    19 复杂请求(非简单请求) 发送两次请求
    20 
    21 
    22 * 简单请求和非简单请求的区别?
    23 
    24    简单请求:一次请求
    25    非简单请求:两次请求,在发送数据之前会先发一次请求用于做“预检”,只有“预检”通过后才再发送一次请求用于数据传输。
    26 * 关于“预检”
    27 
    28 - 请求方式:OPTIONS
    29 - “预检”其实做检查,检查如果通过则允许传输数据,检查不通过则不再发送真正想要发送的消息
    30 - 如何“预检”
    31      => 如果复杂请求是PUT等请求,则服务端需要设置允许某请求,否则“预检”不通过
    32         Access-Control-Request-Method
    33      => 如果复杂请求设置了请求头,则服务端需要设置允许某请求头,否则“预检”不通过
    34         Access-Control-Request-Headers
    35 
    36 
    37 请求的网站响应内容:
    38 def index(request):
    39     a = {'name':'chao'}
    40     ret = JsonResponse(a)
    41     ret["Access-Control-Allow-Origin"] = "http://127.0.0.1:8000" #让http://127.0.0.1:8000这个网址的所有请求都能通过同源机制获得我给他响应的数据
    42     ret["Access-Control-Allow-Origin"] = "*"
    43     ret["Access-Control-Allow-Origin"] = "http://127.0.0.1:8000,http://127.0.0.1:8001,"
    44     ret["Access-Control-Allow-Headers"] = "content-type" #让所有的请求数据格式都能通过同源机制,
    45     return ret

    modelform 通过model的属性自动翻译成form的属性,来进行form组件的工作

     1 from django.core.exceptions import ValidationError
     2 class BookModelForm(forms.ModelForm):
     3     # title=forms.CharField(max_length=15,min_length=6)
     4     
     5     class Meta:
     6         model = models.Book
     7         # fields=['title','publishs',]
     8         fields='__all__'
     9         # exclude = ['title','xx',]
    10 
    11         labels = {
    12             'title':'书名',
    13             'publishDate':'出版日期',
    14         }
    15         widgets = {
    16             'publishDate':forms.widgets.TextInput(attrs={'type':'date'}),
    17         }
    18         error_messages = {
    19             'title':{'required':'不能为空',},
    20             'publishDate':{'required':'不能为空',}
    21         }
    22 
    23     # def clean_title(self):
    24     #     value = self.cleaned_data.get('title')
    25     #     if '666' in value:
    26     #         raise ValidationError('光喊6是不行的!!')
    27     #     else:
    28     #         return value
    29     # def clean(self):
    30     #     ...
    31     #批量添加标签样式
    32     def __init__(self,*args,**kwargs):
    33         super().__init__(*args,**kwargs)
    34         for field in self.fields.values():
    35             field.widget.attrs.update({'class':'form-control'})
  • 相关阅读:
    sharepoint部署
    继承实体类出现传值时值不能保留
    面试经历
    sharepoint更换数据库链接
    asp.net c# 打开新页面或页面跳转
    sharepoint中配置工作流
    AD添加组织单位
    常用正则表达式
    删除多级非空目录
    C#实现对Word文件读写
  • 原文地址:https://www.cnblogs.com/ch2020/p/13365675.html
Copyright © 2020-2023  润新知