localhost attributeee中的Django测试post

2024-10-02 22:33:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个测试.py文件,它应该发送一个POST请求到我的LocalHost站点(如果LH对此不起作用,我也可以对它进行测试)test.domain.com网站). 但是,我没有得到任何新的信息保存在我的数据库。我用GET-before试用过,效果很好。你知道吗

命令中的错误消息:

File "C:\Users\winkl\Desktop\VE\mysite\payment\views.py", line 36, in webhook user = User.objects.POST(id=request.POST('clientAccnum')) AttributeError: 'UserManager' object has no attribute 'POST'

你知道吗视图.py你知道吗

@csrf_exempt
def webhook(request):
    template_name = 'payment/index.html'

    hook = Webhook()
   #ERROR MESSAGE FOR LINE BELOW
    user = User.objects.POST(id=request.POST('clientAccnum'))

    hook.user = user
    hook.clientSubacc = request.POST('clientSubacc')
    hook.eventType = request.POST('eventType')
    hook.eventGroupType = request.POST('eventGroupType')
    hook.subscriptionId = request.POST('subscriptionId')

    hook.timestamp = request.POST('timestamp')
    hook.timestamplocal = timezone.now()

    hook.save()

    user = User.objects.POST(id=request.POST('clientAccnum'))
    hook.user = user
    hook.user.profile.account_paid = hook.eventType == 'RenewalSuccess'
    hook.user.profile.save()

    print (hook.user, hook.clientSubacc, hook.timestamplocal)
    return render(request, template_name)

你知道吗测试.py你知道吗

from django.test import TestCase
import requests
import json

url = 'http://127.0.0.1:8000/payment/webhook/'
data = {'user':'11',
        'clientSubacc': '1111',
        'eventType': 'RenewalSuccess',
        'eventGroupType': 'Success',
        'subscriptionId': '12345'}

r = requests.post(url, (data))

目标是让这个测试帖子创建一个成功的webhook,它将被保存在我的DB中。此信息更新我的用户帐户状态。你知道吗


Tags: pyidobjectsrequestwebhookhookpaymentpost
1条回答
网友
1楼 · 发布于 2024-10-02 22:33:12

重构或smth时出现错误类型。尝试使用

user = User.objects.get(id=request.POST.get('clientAccnum'))

获取请求的用户

添加到答案

这个答案让我更接近,但并没有100%解决问题。你知道吗

  1. 失踪的是“进去”POST.get公司()你会在专栏里看到只有POST()。你知道吗
  2. “user”不是一个真正的对象将其更改为“clientAccnum”

最好打印整个帖子,而不是只打印用户。帮助我认识到第二个问题。你知道吗

我一直被困在这上面!谢谢你的帮助。你让我再往前走了几步,这就把我带到了其他地方。你知道吗

def webhook(request):
    template_name = 'payment/index.html'

    #print(request.POST.get('clientAccnum'))
    print(request.POST)

    hook = Webhook()
    user = User.objects.get(id=request.POST.get('clientAccnum'))
    #user = User.objects.get(id=request.POST.get('user'))

    #hook.user = request.GET.get('clientAccnum')
    hook.user = user
    hook.clientSubacc = request.POST.get('clientSubacc')
    hook.eventType = request.POST.get('eventType')
    hook.eventGroupType = request.POST.get('eventGroupType')
    hook.subscriptionId = request.POST.get('subscriptionId')

    hook.timestamp = request.POST.get('timestamp')
    hook.timestamplocal = timezone.now()

    hook.save()

    user = User.objects.get(id=request.POST.get('clientAccnum'))
    hook.user = user
    hook.user.profile.account_paid = hook.eventType == 'RenewalSuccess'
    hook.user.profile.save()

相关问题 更多 >