首页 > 其他分享 > django中使用form表单或者ajax提交数据时如何验证csrftoken

django中使用form表单或者ajax提交数据时如何验证csrftoken

时间:2023-07-05 17:11:17浏览次数:46  
标签:cookies name form cookieValue ajax cookie 提交 csrftoken

  • 使用form表单来提交数据时,如何验证csrftoken

image

  • ajax提交数据时验证csrftoken

在需要提交的html页面引入以下js文件就行

引入csrf.js文件

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

文件内容:

/**
 * 根据cookie的name获取对应的值
 * @param name
 * @returns {null}
 */
function getCookie(name) {
    let cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        const cookies = document.cookie.split(';');
        for (let i = 0; i < cookies.length; i++) {
            const cookie = cookies[i].trim();
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}

$.ajaxSetup({
    beforeSend: function (xhr, settings) {
        if (!csrfSafeMethod(settings.type)) {
            xhr.setRequestHeader("X-CSRFTOKEN", getCookie('csrftoken'));
        }
    }
})

标签:cookies,name,form,cookieValue,ajax,cookie,提交,csrftoken
From: https://www.cnblogs.com/suncolor/p/17529048.html

相关文章

  • js如何动态清除form表单中input款下的错误信息
    form表单<formaction=""method="post"novalidateid="myform">{%csrf_token%}{%forforminform_obj%}<divclass="form-group"><labelfor="{{form.i......
  • 在linux开发板上加载.ko驱动文件时,出现“insmod: ERROR: could not insert module led
    本文档仅用于本人在学习过程中的记录,方便日后查找问题。问题描述:在ubuntu虚拟机编译出的xxx.ko文件,发送到linux开发板上,执行insmodxxx.ko时,出现“insmod:ERROR:couldnotinsertmoduleled.ko:Invalidmoduleformat”错误。原因查找:1)在linux开发板上,使用uname-r查看lin......
  • .net Core Winform 增加NLog
    nlog.config<?xmlversion="1.0"encoding="utf-8"?><nlogxmlns="http://www.nlog-project.org/schemas/NLog.xsd"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><targetsasync=&quo......
  • 【论文阅读】Pyramid Vision Transformer: A Versatile Backbone for Dense Predictio
    来自ICCV2021论文地址:[2102.12122]PyramidVisionTransformer:AVersatileBackboneforDensePredictionwithoutConvolutions(arxiv.org)代码地址:https://link.zhihu.com/?target=https%3A//github.com/whai362/PVT一、Motivation1.将金字塔结构引入视觉Transformer,使......
  • OSFormer: One-Stage Camouflaged Instance Segmentation with Transformers
    地址:https://arxiv.org/pdf/2207.02255.pdf1.摘要    OSFormer为基于transformer的伪装实例分割(CIS)框架,有两个关键设计,首先是位置敏感transformer(LST),通过【位置引导查询】和【混合卷积前向传播网络】获得定位标签和实例级参数;第二,开发粗糙到精细融合模块(CFF)合并来自LST......
  • form表单验证
    1、典型表单 <el-formref="form":model="form"label-width="80px"><el-form-itemlabel="活动名称"><el-inputv-model="form.name"></el-input></el-form-item><el-form-ite......
  • C# 使用HttpListener时候异常(此平台不支持此操作:System.PlatformNotSupportedExceptio
    C#使用HttpListener时候异常(此平台不支持此操作:System.PlatformNotSupportedException)代码:HttpListenerlistener=newHttpListener();错误:System.PlatformNotSupportedException:OperationisnotsupportedonthisplatformInSystem.Net.HttpListener..ctor()解决办......
  • 预训练模型 | Transformer模型的规模大小
    Transformer有两个模型的规模大小标准:base、big。具体去thumt的models文件夹下的Transformer模型实现可以看到其参数大小。我们可以从Transformer模型的原论文(AttentionIsAllYouNeed)中看到,Transformer有两个模型的规模大小标准:base、big。Transformer模型的超参数Tran......
  • [问题记录] C# string.format null值变量值需要显示在占位符
    起因是在C#程序里执行存储过程,恰好参数值里有NULL值变量,可是null值没有填充到占位符上。网上一看,好多都是添加参数的方法(command.Parameters.Add(),DBNull.value)去解决这个问题,实在不想搞的这么麻烦,我就只想简单点。 比如string.Format(@"EXECXXX{0},{1},{2}",parameter......
  • Jmeter学习之五_跟踪被测试服务器的performance
    Jmeter学习之五_跟踪被测试服务器的performance背景这几天简单学习了一些基本的测试过程.可以实现一些简单基本的功能了.今天晚上继续进行了jmeter的一些学习.想着可以在测试人大金仓的同时可以查看一下本地的机器性能.用到的工具以及资料https://www.cnblogs.com/......