Django:GET请求工作正常,但POST请求中的数据发送不起作用

1cosmwyk  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(178)

我再次向这个url http://127.0.0.1:8000/user_ques_management/add_favorite_question/发送了一个POST,上面有这个JSON正文:

{
  "idname": "aallen",
  "question_id": 201
}

字符串
得到响应:

{
    "error": "User not found."
}


在终端里也看到了这个:收到的POST数据:<QueryDict:{}>收到的idname:未找到:/user_ques_management/add_favorite_question/
但是,当我发送一个GET请求与相同的用户用户ID到这个url http://127.0.0.1:8000/user_ques_management/favorite_read_questions/3486/:我得到了成功的回应:

{
    "user_id": 3486,
    "display_name": "Dustin Rodriguez",
    "favorite_questions": [],
    "read_questions": []
}


我看到用户信息也在数据库中。下面是我的UserProfile模型:

class UserProfile(models.Model):
    idname = models.CharField(max_length=250, unique=True)
    display_name = models.CharField(max_length=250)
    email = models.EmailField()
    phone = models.CharField(max_length=20)

    def __str__(self):
        return self.display_name


Views.py 对于两个URL:视图以插入用户收藏的问题

@csrf_exempt
def add_favorite_question(request):
    if request.method == 'POST':
        print("Received POST data:", request.POST)
        idname = request.POST.get('idname')  # Use 'idname' instead of 'user_id'
        question_id = request.POST.get('question_id')
        print("Received idname:", idname)  # Print the received idname

        try:
            user = UserProfile.objects.get(idname=idname)
            question = Question.objects.get(id=question_id)

            FavoriteQuestion.objects.create(user_id=user, question_id=question)
            return JsonResponse({'message': 'Favorite question added successfully.'})
        except UserProfile.DoesNotExist:
            return JsonResponse({'error': 'User not found.'}, status=404)
        except Question.DoesNotExist:
            return JsonResponse({'error': 'Question not found.'}, status=404)


查看以检索用户的收藏和阅读问题

def get_favorite_and_read_questions(request, user_id):
    try:
        user = UserProfile.objects.get(id=user_id)
        favorite_questions = FavoriteQuestion.objects.filter(user_id=user).values('question_id')
        read_questions = ReadQuestion.objects.filter(user_id=user).values('question_id')

        favorite_question_ids = [item['question_id'] for item in favorite_questions]
        read_question_ids = [item['question_id'] for item in read_questions]

        response_data = {
            'user_id': user.id,
            'display_name': user.display_name,
            'favorite_questions': favorite_question_ids,
            'read_questions': read_question_ids,
        }

        return JsonResponse(response_data)
    except UserProfile.DoesNotExist:
        return JsonResponse({'error': 'User not found.'}, status=404)


为什么GET请求显示用户信息,而POST请求显示未找到用户?此外,还有一些我使用Faker生成的虚拟数据,后来加载到sqlite数据库中。

fcwjkofz

fcwjkofz1#

代码看起来不错。它似乎没有正确接收请求。您是否还可以包含请求的代码?QueryDict似乎是空的。另外,当你检查方法时,你实际上是通过一个get请求来获取数据的,这是非常奇怪的。为了避免检查,你也可以使用装饰器顺便说一句!也是

from django.views.decorators.http import require_http_methods
@csrf_exempt
@require_http_methods(["POST"])
def my_view(request):
    pass

字符串

相关问题