如何在 django 中使用 $.post ?

发布于 2024-10-25 07:51:11 字数 342 浏览 4 评论 0原文

如何在 Django 中使用 jquery.post() 方法?

这就是我想做的:

         var postdata={
              'username':$('#login-email').val(), 
              'password':$('#login-password').val()
         }

         $.post('/login/',postdata)

How do I CSRF protected this in django?有没有办法将 CSRF 令牌添加到发布数据中?

How can I use the jquery.post() method in Django?

This is what I am trying to do:

         var postdata={
              'username':$('#login-email').val(), 
              'password':$('#login-password').val()
         }

         $.post('/login/',postdata)

How do I CSRF protect this in django? Is there a way to add to the CSRF token to the post data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

满意归宿 2024-11-01 07:51:11

是的。我相信它存储在 {{ csrf_token }} 中。因此,

     var postdata={
          'username':$('#login-email').val(), 
          'password':$('#login-password').val(),
          'csrfmiddlewaretoken': '{{ csrf_token }}'
     }

您可能需要仔细检查名称,但这应该是正确的。

Yes. I believe it's stored in {{ csrf_token }}. So, just do

     var postdata={
          'username':$('#login-email').val(), 
          'password':$('#login-password').val(),
          'csrfmiddlewaretoken': '{{ csrf_token }}'
     }

You might have to double check the names, but that should be right.

酸甜透明夹心 2024-11-01 07:51:11

我通常将包含此内容的文件引用到我希望能够发出 AJAX 请求的每个页面:

if (!$)
    var $ = django.jQuery;

$('html').ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = $.trim(cookies[i]);
                // 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;
    }
    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        // Only send the token to relative URLs i.e. locally.
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});

I usually refer a file with this content to every page I want to be able to make AJAX requests:

if (!$)
    var $ = django.jQuery;

$('html').ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = $.trim(cookies[i]);
                // 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;
    }
    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        // Only send the token to relative URLs i.e. locally.
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});
爱她像谁 2024-11-01 07:51:11

尽管您没有在示例中提供 html,但可以安全地假设您使用的是

吗?如果是这样,请将 CSRF 令牌模板标记添加到表单中,并在表单上调用 .serialize()

Although you didn't provide your html in your example is it safe to assume that you are using a <form>? If so, add your CSRF token template tag to your form and call .serialize() on your form.

我们只是彼此的过ke 2024-11-01 07:51:11

Django 中的 contrib 模块有一个 CSRF 模块 您可以使用。

至于您的问题如何发送POST,只要您正确映射了URL,请求就会发送到它。您可以通过检查请求对象上的 request.POST 来专门处理 POST 请求。

The contrib module in Django has a CSRF module that you can use.

As to your question how to send a POST, as long as you have the URL mapped properly requests will get sent to it. You can handle a POST request specifically by checking request.POST on the request object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文