如何制作采用POST数据的Django-Rest-Framework API? [英] How to make a Django-Rest-Framework API that takes POST data?

查看:526
本文介绍了如何制作采用POST数据的Django-Rest-Framework API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django-Rest-Framework API构建一个Django应用程序。
我已经建立了API端点,如下所示。我想能够从我的浏览器POST数据。我想从我的数据库中检索一个具有匹配的主要对象模型,如URL中给出的。而且我想根据浏览器发布的数据对该检索对象进行操作。如果我可以从我的ViewSet抓住发布的数据,我会完成。但是,当我做POST时,我不知道如何执行该viewset的update()函数。



从我的urls.py文件:

  router.register(r'replyComment',views.ReplyComment,base_name =replyComment)
/ pre>

从我的views.py文件:

  class ReplyComment (viewets.ViewSet):
def update(self,request,pk = None):
try:
origComment = Comment.objects.get(pk = pk)
#这里的东西修改origComment的状态并保存它。
返回响应(
json.dumps(True),
status = status.HTTP_200_OK,

除了异常作为异常:
logger.error例外)
返回响应(status = status.HTTP_400_BAD_REQUEST)

我正在使用高级我的Chrome浏览器中的客户端(ARC)工具。当我使用POST方法将ARC工具指向 http://127.0.0.1:3001/api/replyComment/2/ 时,我收到错误:

  {
detail:CSRF Failed:CSRF token missing or incorrect。
}

查看截图 这里 。看起来我在这里用POST做错了。有人能告诉你如何正确地做到这一点吗?如何解决我的CSRF问题?我是Django Rest框架的新手。所以如果你能提供清晰的细节,那将是非常感谢。请让我知道我需要做什么改变,以确保我的POST工作,因为我打算?我需要更多的帮助,而不是简单地指我的手册。 (我试过,但仍然无法使其工作)

解决方案

CSRF令牌在Django中是必需的,以防止CSRF网站请求伪造)。对于写入某些东西(POST,PUT,DELETE等)的方法,您需要在请求中包含一个CSRF令牌,以便Django知道您的站点的请求。



您可以在 Django-rest-框架文档。正如文档中所述,您可以在 Django文档


I'm building a Django application with Django-Rest-Framework APIs. I have built an API endpoint as follows. I want to be able to POST data from my browser. I want to retrieve an object model from my Database that has the matching primary as given in the URL. And I want to operate on that retrieved object based on the data posted by the browser. If I could just grab the posted data from with my ViewSet, I would be done. But I don't know how to get to execute that viewset's update() function when I do a POST.

From my urls.py file:

router.register(r'replyComment', views.ReplyComment, base_name="replyComment")

From my views.py file:

class ReplyComment(viewsets.ViewSet):
    def update(self,request,pk=None):
        try: 
            origComment = Comment.objects.get(pk=pk)
            # Do something here that modifies the state of origComment and saves it.
            return Response(
                json.dumps(True), 
                status=status.HTTP_200_OK,
            )
        except Exception as exception:
            logger.error(exception)
            return Response(status=status.HTTP_400_BAD_REQUEST)

I'm using the Advanced Rest Client (ARC) tool in my Chrome browser. When I point the ARC tool to http://127.0.0.1:3001/api/replyComment/2/ using the POST method, I get the error:

{
    detail: "CSRF Failed: CSRF token missing or incorrect". 
}

See the screenshot here. It seems like I'm doing something wrong here with my POST. Can someone please advise how to do this properly? How can I get around my CSRF issue? I'm a newbie to Django Rest Frameworks. So if you can provide clear details, it would be most appreciated. Please let me know what changes I need to make to ensure my POST works as I intend it to? I need a bit more help than simply referring me to the manual. (I tried that but still couldn't make it work)

解决方案

CSRF Tokens are required in Django to protect against CSRF(Cross Site Request Forgery). For methods that writes something (POST, PUT, DELETE etc), you need to include a CSRF token with your request so that Django knows the request came from your own site.

You can read more about in Django-rest-framework documentation. And as it says in the doc, you can find how to include the CSRF token in the HTTP Header in Django documentation.

这篇关于如何制作采用POST数据的Django-Rest-Framework API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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