如何将Django HttpResponse转换为Django渲染调用 [英] How to convert a Django HttpResponse to a Django render call

查看:108
本文介绍了如何将Django HttpResponse转换为Django渲染调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

  def ajax_login_request(request):
try:
请求。 POST [u'login']
dictionary = request.POST
except:
dictionary = request.GET
user = authenticate(username = dictionary [u'login'],password = dictionary [u'password'])
如果user和user.is_active:
登录(请求,用户)
result = True
else:
result = False
response = HttpResponse(json.dumps(result),mimetype = u'application / json')
返回响应

这是通过ajax调用的。我是一个noob,这是一本书中的例子。不幸的是,我使用的Django的版本会在此引发一个CSRF错误。我已经完成了其他CSRF位,但是我不知道如何将HttpResponse位更改为render调用。我不想使用CSRF_exempt,因为我不知道什么时候适合。有人可以为我提供上述HttpResponse的等效渲染调用。



谢谢

解决方案

要使原始代码工作,您需要获取一个RequestContext对象并将其与您的响应一起传递,如下所示:

 code> from django.http import HttpResponse 
from django.template import RequestContext,Template

def ajax_login_request(request):
#...

#这一段代码将CSRF位添加到您的请求中。
c = RequestContext(request,{'result':json.dumps(result)})
t = Template({{result}})#一个虚拟模板
response = HttpResponse $。





$ / code $请阅读 CSRF文档,因为您可能会遇到奇怪的错误如果您不明白CSRF在您的应用程序中有线的所有方式。该页面还有一个javascript代码片段,以确保在没有表单的情况下发送它们时,您的ajax请求将发送CSRF cookie。



您还可以使用 render_to_response()快捷方式,但您需要具有一个实际的模板加载(在你的情况下,你不需要一个模板,因此我的例子中的虚拟模板)。


I have the following code

def ajax_login_request(request):
   try:
      request.POST[u'login']
      dictionary = request.POST
   except:
      dictionary = request.GET
   user = authenticate(username = dictionary[u'login'], password = dictionary[u'password'])
   if user and user.is_active:
      login(request, user)
      result = True
   else:
      result = False
   response = HttpResponse(json.dumps(result), mimetype = u'application/json')
   return response

which is being called via ajax. I'm a noob and this is from an example in a book. Unfortunately the version of Django I'm using throws a CSRF error on this. I've done the other CSRF bits, but I don't know how to change the HttpResponse bit to a render call. I do not want to use CSRF_exempt, because I have no idea when that is appropriate. Can someone please provide me the equivalent render call for the HttpResponse above.

Thanks

解决方案

To make your original code work, you need to get a RequestContext object and pass it along with your response, something like this:

from django.http import HttpResponse
from django.template import RequestContext, Template

def ajax_login_request(request):
   # ...

   # This bit of code adds the CSRF bits to your request.
   c = RequestContext(request,{'result':json.dumps(result)})
   t = Template("{{result}}") # A dummy template
   response = HttpResponse(t.render(c), mimetype = u'application/json')
   return response

Do read up on the CSRF documentation as you might run into strange errors if you don't understand all the ways CSRF is "wired" in your app. The page also has a javascript snippet to make sure CSRF cookies are sent with your ajax requests if you are sending them without a form.

You can also use the render_to_response() shortcut, but you would need to have an actual template to load (in your case, you don't need a template, hence the "dummy" template in my example).

这篇关于如何将Django HttpResponse转换为Django渲染调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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