【问题标题】:How can I hit Django api with parameters using Postman?如何使用 Postman 使用带有参数的 Django api?
【发布时间】:2019-09-29 10:08:18
【问题描述】:
@must_be_admin_user
def patch(self,request,category_id):
    '''
    Updates Data in the Category
    Parameters
    Authenticated Admin User , category_id
    '''
    category = get_object_or_404(Category, id=category_id)

   # IF admin is not the Creater of the Category     
    if category.created_by != request.user.id:
        Response(status=status.HTTP_400_BAD_REQUEST, data={"error": "You did not Create this so you cant delete this"})

    category.last_updated = timezone.now()

    updated_category = CategorySerializer(
        category, data=request.data, partial=True)
    if updated_category.is_valid():
        updated_category.save()
        return Response(status=status.HTTP_201_CREATED, data=updated_category.data)

    return Response(status=status.HTTP_400_BAD_REQUEST, data={"error": updated_category.errors})

我想知道如何使用 postman 发送 category_id 。 我在使用邮递员访问此 api 时遇到问题

【问题讨论】:

    标签: django python-3.x testing django-rest-framework postman


    【解决方案1】:

    如果调用此视图的 URL 模式,例如“patch”,您可以:

    from django.urls import reverse
    # Use the resulting url in Postman. This is what I use in tests.
    url = reverse('patch', args=(some_id_here,), kwargs={})
    

    您可以将这种技术用于您拥有的每个视图。

    更多here

    您必须注意的另一件事是执行此请求的用户必须以管理员身份登录,因此您已相应地设置了 Postman cookie。

    我建议使用名为“Postman Interceptor”的谷歌浏览器扩展程序

    祝你好运!

    【讨论】:

      猜你喜欢
      • 2016-03-14
      • 1970-01-01
      • 2022-10-26
      • 2015-05-29
      • 2021-04-13
      • 1970-01-01
      • 2020-10-17
      • 2015-09-10
      相关资源
      最近更新 更多