【问题标题】:"<field>":["This field is required."] message Django REST framework"<field>":["This field is required."] message Django REST framework
【发布时间】:2021-02-28 20:04:22
【问题描述】:

所以今天我第一次尝试在我的项目中实现 Django REST 框架,一切正常我可以使用框架提供的浏览器界面创建、更新和删除帖子,但是在集成了JWT 令牌并尝试使用 curl 创建帖子我总是收到消息 "":["This field is required."] 。我尝试以多种方式对其进行故障排除,但无法解析我需要正确解析的字段。我什至可以使用 curl 创建一个帖子,但我必须将字段修改为全部为“空值”。我是否发送了错误的 curl 请求?

curl: (请注意,如果我添加 -H "Content-Type: application/json" 我得到这个输出 {"detail":"JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"} 已经在这里解决了 Json parse error using POST in django rest api 通过删除内容类型标题)编辑:忽略我刚才所说的内容类型标题,这是我的一个误解

 curl -X POST -H  "Authorization: JWT <token>" -d '{
                    "title": "Helloooo",
                    "content": "Hi",
                    "schools": null,
                    "course": null,
                    "classes": [
                        1
                    ],
                    "isbn": 12312,
                    "semester": null,
                    "visible": false

}' 'http://127.0.0.1:8000/api/posts/create/?type=post'

这是我在发送 POST 请求后从终端得到的输出:

 {"title":["This field is required."],"content":["This field is required."],"classes":["This list may not be empty."]}

这是我的其余代码:

序列化器:

class PostCreateSerializer(ModelSerializer):

    date_posted = serializers.HiddenField(default=timezone.now)

 class Meta:
        model = Post
        fields = [
 "title",
 "content",
 "schools",
 "course",
 "classes",
 "isbn",
 "semester",
 "visible",
 "date_posted",
        ]

观看次数:

class PostCreateAPIView(CreateAPIView):
    queryset = Post.objects.all()
    serializer_class = PostCreateSerializer

 def perform_create(self, serializer):
        serializer.save(author=self.request.user)

网址:

urlpatterns = [
    path(r"", PostListAPIView.as_view(), name="List-API"),
    path("create/", PostCreateAPIView.as_view(), name="Create-API") ]

设置:

REST_FRAMEWORK = {
    "DEFAULT_RENDERER_CLASSES": [
        "rest_framework.renderers.JSONRenderer",
        "rest_framework.renderers.BrowsableAPIRenderer", 
    ],
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework.authentication.SessionAuthentication", 
        "rest_framework_jwt.authentication.JSONWebTokenAuthentication"
    ],
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated"  
    ],
}

感谢您花时间阅读所有这些内容!

【问题讨论】:

  • 这个问题可能在windows中。是真的吗?
  • 你的意思是操作系统窗口吗?是的,我正在使用 powershell 7 发出请求
  • 我编辑了我的答案。问题在于在 Windows 操作系统中使用单引号作为正文。

标签: django curl django-rest-framework request


【解决方案1】:

问题可能出在您的 curl 请求中。
我建议改用 Postman

但是如果你想使用 curl 来发送 JSON 内容类型的 body,你必须在请求头中确定它。
在 Windows 操作系统中,您无法使用单引号确定 JSON 正文。 所以试试这个:

curl -X POST \
-H  "Authorization: JWT <token>" \
-H "Content-Type: application/json" \
-d "{
                    \"title\": \"Helloooo\",
                    \"content\": \"Hi\",
                    \"schools\": null,
                    \"course\": null,
                    \"classes\": [
                        1
                    ],
                    \"isbn\": 12312,
                    \"semester\": null,
                    \"visible\": false

}" http://127.0.0.1:8000/api/posts/create/?type=post

【讨论】:

  • 谢谢!我已经解决了我的问题,发表评论澄清一切。
【解决方案2】:

感谢 Seyyed Sajjad Sanikhani,我最终解决了我的问题,我实际上需要通过在 powershell 中使用正确的引号和填充来指定内容类型,最终代码是:

curl -X POST -H  "Authorization: JWT <token>" -H "Content-Type: application/json" -d '{
                 \"title\": \"Hello there\",
                     \"content\": \"high ground\",
                     \"schools\": "1",
                     \"course\": "1",
                     \"classes\": [
                         "1"
                     ],
                     \"isbn\": 12312,
                     \"semester\": "1",
                     \"visible\": true

 }' 'http://127.0.0.1:8000/api/posts/create/?type=post'

然后最后的输出是:

{"title":"Hello there","content":"high ground","schools":1,"course":1,"classes":[1],"isbn":12312,"semester":1,"visible":true}

所以请小心请求,因为您最终可能会收到以下错误:

"detail": "JSON parse error - Expecting property name enclosed in double-quotes"

"[globbing] nested brace in column"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 2022-11-07
    • 1970-01-01
    相关资源
    最近更新 更多