Django jQuery发帖请求 [英] Django jQuery post request

查看:125
本文介绍了Django jQuery发帖请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $。ajax({
url:'/',
type:POST,
data:{name:'name'年龄:'age'},
success:function(response){},
complete:function(){},
error:function(xhr,textStatus,thrownError){}
});

而且在views.py:

  class SomeView(generic_views.TemplateView):
template_name ='something.html'

def get(self,request,* args,** kwargs) :
... something ...
return self.render_to_response(context)

def post(self,request,* args,** kwargs):
name = request.POST ['name']
age = request.POST ['age']
... something ...

我得到: [05 / Oct / 2012 12:03:58]POST / something / HTTP / 1.1403 2294 p>

我想通过jQuery将此数据(名称和年龄)发送到SomeView中的此帖子功能。这是与加载的模板相同的视图,只是请求类型不同。在get()模板加载和post,post()函数应该被调用。可能吗?我已经检查过其他问题,并得到了这个解决方案。它应该是工作。我做错了什么?

解决方案

你的问题的答案是你的错误,是:不是很多! >

如果传入的POST请求失败,Django将返回403响应(禁止)Csrf检查。
您可以通过jQuery的ajaxSetup执行此操作,可以找到代码片段此处



这个工作在GET请求的原因,只是GET请求不被csrf中间件检查。



<因为你似乎在这里建立一个表单,另外要考虑的是使用基于类的表单。他们处理get / post和参数验证。井井有条。特别是当您制作表单来编辑/创建/删除模型实例时,您可以拥抱 ModelForms 和CreateViews。非常整齐。



可能需要一些时间才能获得那些基于类的泛型视图的挂起。但这非常值得。


$.ajax({
    url:'/',
    type: "POST",
    data: {name: 'name', age: 'age'},
    success:function(response){},
    complete:function(){},
    error:function (xhr, textStatus, thrownError){}
});

And in views.py:

class SomeView(generic_views.TemplateView):
    template_name = 'something.html'

    def get(self, request, *args, **kwargs):
        ...something...
        return self.render_to_response(context)

    def post(self, request, *args, **kwargs):
        name = request.POST['name']
        age = request.POST['age']
        ...something...

And I get: [05/Oct/2012 12:03:58] "POST /something/ HTTP/1.1" 403 2294

I'd like to send this data(name and age) via jQuery to this post function in "SomeView". This is the same view as the loaded template, just the request type is different. On get() the template loads and on post, the post() function should be called. Is it possible? I've checked other questions and got this solution. It was supposed to be working. What am I doing wrong?

解决方案

The answer to your question what you are doing wrong, is: not much!

Django returns a 403 response (Forbidden) if an incoming POST request fails Csrf checks. You can do this through jQuery's ajaxSetup, code snippets are found here

The reason that this DOES work on a GET request, is simply that GET requests are not checked by the csrf middleware.

As it seems you are building a form here, another thing to consider is using class based forms. They handle get/post and also parameter validation for you. Very neat. Especially when you are making forms to edit/create/delete model instances, in which case you can embrace the power of ModelForms and CreateViews. Very neat.

It might take some time to get the hang of those generic class based views. But it's very well worth it.

这篇关于Django jQuery发帖请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆