简体   繁体   中英

Unable to retrieve HTTP Post data from external API in django

I am getting error : 'str' object has no attribute 'method' . See my code below :

@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'})

In summary I am trying to accept and use HTTP POST value from an external API. Value is showing when I inspect element but DJANGO not retrieving. Please help.

Here is my urls.py

from django.conf.urls import patterns, url
from views import *
from django.views.generic import RedirectView
urlpatterns = patterns('myproject.prelude.views',
# Home:
url(r'^$', 'home', name='home'),

#login
url(r'^login/$', 'login', name='login'),
url(r'^welcome/$', 'welcome', name='welcome'),

# Registration Portal
# Registration Portal
url(r'^uportal/$', 'uportal', name='uportal'),
url(r'^uportal/ugreg/find/$', 'findmember', name='findmember'),
url(r'^uportal/ugreg/search/$', 'searchmember', name='searchmember'),
url(r'^uportal/ugreg/$', 'ugreg', name='ugreg'),
url(r'^uportal/ugreg/initiate-payment/$', 'initiatepayment', name='initiatepayment'),
url(r'^uportal/ugreg/verifypayment/$', 'verifypayment', name='verifypayment'),
url(r'^uportal/ugreg/proceedpayment/$', RedirectView.as_view(url='https://bestng.com/pay'), name='remote_admin'),
url(r'^uportal/ugreg/completepayment/$', completepayment, name='completepayment'),

Thank you

It appears that your problem is that request is an str object rather than a request object.

Please produce urls.py and views.py.

For readability, let's rewrite the part below:

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

Or even, like so:

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

Also, the try block is not what I would use. Instead, I would use the get method of the POST dict and return a flag value:

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

I am unable to reproduce the error you are getting. With a slightly different project-level urls.py (very simplified), the 'completepayment' view works fine for me. Here is urls.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM