简体   繁体   English

Django-rest-framework:可浏览的API无法通过HTML表单获取POST数据,但可以从Raw数据获取

[英]Django-rest-framework: Browsable API can't get POST data via HTML form, but can from Raw data

Well, the problem is simple. 好吧,问题很简单。 If I post my data via the HTML form, a new record is created in the DB but all data passed is null, as shown in the picture. 如果我通过HTML表单发布数据,那么将在数据库中创建一条新记录,但是传递的所有数据均为空,如图所示。

post response via html form 通过html表单发布回复

However, if a use the "raw_data" interface, everything works just fine: 但是,如果使用“ raw_data”接口,则一切正常:

post response via raw data 通过原始数据发布回复

Code is quite simple: 代码很简单:

views.py: views.py:

class CollectionList(generics.ListCreateAPIView):
    queryset = Collection.objects.all()
    serializer_class = CollectionSerializer

serializer.py: serializer.py:

class CollectionSerializer(serializers.ModelSerializer):
  class Meta:
    model = Collection
    fields = ('id','title', 'nlikes')  

Any suggestion? 有什么建议吗?

You simply need to upgrade django rest framework to 3.3.1. 您只需要将django rest框架升级到3.3.1。 There was a bug in 3.3.0 that sets all form data to empty values when using the session login. 3.3.0中存在一个错误,使用会话登录名时会将所有表单数据设置为空值。

Edit: The issue was https://github.com/tomchristie/django-rest-framework/issues/3574 编辑:问题是https://github.com/tomchristie/django-rest-framework/issues/3574

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 django-rest-framework 中显示 HTML 个表而不是原始数据 - Show HTML tables in django-rest-framework instead raw data Django Rest Framework 如何在可浏览的 API 上发布数据 - Django Rest Framework how to post data on the browsable API 如何制作一个采用POST数据的Django-Rest-Framework API? - How to make a Django-Rest-Framework API that takes POST data? Chrome无法在Django-Rest-Framework可浏览API中呈现Unicode - Unicode not rendering in Django-Rest-Framework browsable API in Chrome django rest 框架 - 用户名字段未以可浏览的 api html 形式呈现 - django rest framework - username field not rendering in browsable api html form Django REST 框架:HTML 形式在可浏览 ZDB974238714CA8DE634A7CE1DF038 中的错误 - Django REST framework: error in HTML form in browsable API 在Django-Rest-Framework中获取嵌套数据时,可以更改related_name吗? - Can I change the related_name when I get the nested data in Django-Rest-Framework? 如何在可浏览的Django Rest API中隐藏HTML表单中的字段? - How to hide a field from the HTML form in the browsable Django Rest API? 在 Django-Rest-Framework POST 调用中,它更新了多对多字段,适用于 JSON 但不适用于表单数据 - In Django-Rest-Framework POST call which updates manytomany field works with JSON but not with form-data 在 django-rest-framework 中解析多部分/表单数据 - Parsing multipart/form-data in django-rest-framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM