无法从djang中的外部API检索HTTP Post数据

2024-10-02 12:34:37 发布

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

我收到错误:“str”对象没有属性“method”。请参阅下面的代码:

@csrf_exempt
def completepayment(request):
    varerr =''
    plist = []

    if request.method == 'POST':
        try:
            nid = request.POST['txnref']

        except MultiValueDictKeyError:
            varerr ="Woops! Operation failed due to server error. Please try again later."
            return render(request, 'uportal/main.html', {'varerr':varerr})
        # Fetching member details
        trym = Transactions.objects.get(TransRef=nid)
        amount = trym.Amount

        famt = int(amount * 100)        

        product_id = 48
        salt = '4E6047F9E7FDA5638D29FD'
        hash_object = hashlib.sha512(str(product_id)+str(nid)+str(famt))
        hashed = hash_object.hexdigest()
        url = 'https://bestng.com/api/v1/gettransaction.json?productid=pdid&transactionreference=nid&amount=famt'
        raw = urllib.urlopen(url)
        js = raw.readlines()
        #js_object = simplejson.loads(js)
        res = simplejson.dumps(js)
        for item in res:
            rcode = item[0]
            #rdesc = item[1]
            #preff = item[2]

            thisresp = completepayment(rcode)
            plist.append(thisresp)

    else:
        varerr ="Woops! Operation failed due to server error. Please try again later."

    return render(request, 'uportal/main.html', {'plist':plist, 'varerr':varerr, 'completepayment':'completepayment'})

总之,我尝试从外部API接受和使用httppost值。值在我检查元素时显示,但DJANGO没有检索。请帮忙。在

这是我的网址.py在

^{pr2}$

谢谢你


Tags: objectrequestjsitempostamountmethodplist
1条回答
网友
1楼 · 发布于 2024-10-02 12:34:37

您的问题似乎是请求是str对象而不是请求对象。在

请出示网址.py以及视图.py. 在

为了可读性,我们重写以下部分:

url    = 'https://bestng.com/api/v1/gettransaction.json'
params = '?productid={product_id}&transactionreference={nid}&amount={famt}'
raw    = urllib.urlopen(url + params.format(**locals()))

或者甚至是这样:

^{pr2}$

另外,try块不是我要使用的。相反,我将使用POST dict的get方法并返回一个标志值:

nid = request.POST.get('tnxref', False)

我无法重现你得到的错误。项目级别略有不同网址.py(非常简单),“completepayment”视图对我来说很好。给你网址.py. 在

from django.conf.urls import patterns, url
from app.views import completepayment
# The app is simply called app in my example.


urlpatterns = patterns('',
    # I remove the prefix
    url(r'^uportal/ugreg/completepayment/$', completepayment, name='completepayment'),
)
# This last parenthesis might be missing in your code.

相关问题 更多 >

    热门问题