1

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

Femi Adigun
  • 59
  • 2
  • 10

1 Answers1

0

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.
Ben Beirut
  • 733
  • 3
  • 12
  • Hello Ben, Thank you for your time, your hint is very useful, but that is not actually the problem. The error is occuring at : nid = request.POST['txnref']. I can't get the view to read the post data from an external API not to talk of using the nid value in your answer below. Thanks so much for your time. Kindly assist me, it wil be greatly appreciated. – Femi Adigun May 24 '15 at 09:27
  • Please provide urls.py and views.py so readers can further assist. – Ben Beirut May 24 '15 at 17:08
  • Hi Ben, I have done that. The initial post is my views.py and I just added my urls.py now. if the request is a string object and not request object, please how do I make my views accept the string object. I am trying to accept the post request from an external API. I can see the posted values when I did inspect element->Network->Params from my browser. Thanks a lot – Femi Adigun May 24 '15 at 21:50
  • I am unable to reproduce the error using django 1.7 on python 3.4. I used a different urls.py because I do not have the rest of the views but what you are doing seems to make sense and I fail to see the reason for it not to work. – Ben Beirut May 27 '15 at 16:09