zarinpal-django-py3
zarinpal-django-py3 copied to clipboard
Error in django view of sending request to zarinpal
It seems in this line there is an error: Send request view
because I could not send the request and it raises an attribute error: '''
AttributeError at /zarinpal/request/
'dict' object has no attribute 'status_code'
Request Method: | GET |
---|---|
http://127.0.0.1:8000/zarinpal/request/ | |
4.2.1 | |
AttributeError | |
'dict' object has no attribute 'status_code' | |
/home/rahim/.local/share/virtualenvs/bookstore-NVJ35xj2/lib/python3.10/site-packages/debug_toolbar/panels/history/panel.py, line 77, in generate_stats | |
zarinpal.views_zarin.send_request | |
/home/rahim/.local/share/virtualenvs/bookstore-NVJ35xj2/bin/python | |
3.10.6 | |
''' |
dorod eror ro fix kardi ? eror man ine : AttributeError at /request/ 'dict' object has no attribute 'headers' baraye in khat code fek konam : response = requests.post(ZP_API_REQUEST, data=data,headers=headers, timeout=10)
Hi Friends,
To fix this, I made two key changes:
- I replaced the dictionary responses with
JsonResponse
to ensure the HTTP responses were properly formatted and could be easily handled on the frontend. - I used
HttpResponseRedirect
to automatically redirect the user to the payment page after a successful request.
Updated code:
from django.http import HttpResponseRedirect, JsonResponse, HttpResponse
try:
response = requests.post(ZP_API_REQUEST, data=data, headers=headers, timeout=10)
if response.status_code == 200:
response = response.json()
if response["Status"] == 100:
return HttpResponseRedirect(ZP_API_STARTPAY + str(response["Authority"]))
else:
return JsonResponse({"status": False, "code": str(response["Status"])})
return HttpResponse(response.content, status=response.status_code)
except requests.exceptions.Timeout:
return JsonResponse({"status": False, "code": "timeout"})
except requests.exceptions.ConnectionError:
return JsonResponse({"status": False, "code": "connection error"})