I am new to django. I am making a website for a customer. I am integrating a paypal client side module and followed a video from youtube for the purpose. On order completion, I am trying to go to a page and I am passing it product id so it can retrieve it from the database and display a nice thank you page. But I am getting the following error:
NoReverseMatch at /product-details/payment
Reverse for ‘order_successful’ with no arguments not found. 1 pattern(s) tried: [‘order_success/(?P[^/]+)$’]
Following is my page checkout.html from where I am calling the function:
<script>
function getCookie(name) {
let cookieValue = null;
if (document.cookie && document.cookie !== '') {
const cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i].trim();
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) === (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
const csrftoken = getCookie('csrftoken');
var total = '{{price}}'
var quantityBought = '{{quant}}'
var prodId = '{{prod.id}}'
var fName = '{{firstName}}'
var lName = '{{lastName}}'
var apt = '{{apt}}'
var street = '{{street}}'
var city = '{{city}}'
var state = '{{state}}'
var zipcode = '{{zipcode}}'
var country = '{{country}}'
var email = '{{email}}'
var phone = '{{phone}}'
async function completeOrder(){
var url = "{% url 'paymentComplete' %}"
const response = await fetch(url, {
method: 'POST',
headers:{
'Content-type': 'application/json',
'X-CSRFToken': csrftoken,
},
body:JSON.stringify({'prodID': prodId, 'quantity': quantityBought, 'bill': total, 'fName': fName, 'lName': lName, 'apt': apt, 'street': street, 'city': city, 'state': state, 'zipcode': zipcode, 'country': country, 'email': email, 'phone': phone})
})
return response.json();
}
createOrder: function(data, actions) {
// This function sets up the details of the transaction, including the amount and line item details.
return actions.order.create({
purchase_units: [{
amount: {
value: '0.50' /*total*/
}
}]
});
},
onApprove: function(data, actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function(details) {
// This function shows a transaction success message to your buyer.
completeOrder()
.then( data => {
alert(data)
});
//alert(data)
window.location.href = "{% url 'order_successful' DATA=prod.id %}"
});
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
</script>
my main.urls.py is as follows:
from django.conf.urls import include
from django.urls import path
from . import views
#path(name_displayed_in_url, rendering_function, tag_name)
urlpatterns = [
path('', views.home, name='home'),
path('all-products', views.all_products, name='all-products'),
path('request-a-quote', views.request_quote, name = 'RequestQuote'),
path('contact-us', views.contact_us, name='ContactUs'),
path('about', views.about, name='about'),
path('product-details/<int:ID>', views.prod_temp, name='prod_temp'),
path('ContactUs', views.ContactUs, name='contact-us'),
path('calcQuote', views.calcQuote, name='calculate-quote'),
path('product-details/purchase', views.purchase, name='purchase'),
path('product-details/payment', views.payment, name='payment'),
path('product-details/paymentComplete', views.paymentComplete, name='paymentComplete'),
path('order_success/<int:DATA>', views.order_successful, name='order_successful'),
]
following is my paymentComplete.views:
def paymentComplete(request):
body = json.loads(request.body)
prod_ID = int(body['prodID'])
prod_Qt_bought = int(body['quantity'])
bill_paid = str(body['bill'])
#loading product from store
prod_from_store = Product.objects.get(id=prod_ID)
#previous product quantity
previous_Qt = int(prod_from_store.left)
#amount to be left in store
newLeft = previous_Qt - prod_Qt_bought
return JsonResponse(prod_ID, safe=False)
and order_successful.view
def order_successful(request, DATA):
return render(request, 'order_successful.html', {'DATA':DATA})
following is completeOrder function in checkout.html from where i am calling paymentComplete function in views.py and sending it a post request
**I have narrowed down the problem to urls.py file because if I call order_successful without passing any parameters, I get my page successfully, so its either a wrong way that i am confused about regarding the writing of the urls, or its something wrong I am doing in the JS script where I am calling the function. **
Advertisement
Answer
I have the impression you need to sort out your problem – there is too many things envolved and they do not fit to the error message.
Did you really post the actual versions of your files (urls.y …)?
Your error message:
NoReverseMatch at /product-details/payment
Reverse for 'order_successful' with no arguments not found. 1 pattern(s) tried: ['order_success/(?P[^/]+)$']
I try to explain:
- the first line of the error message indicates that you called a url /product-details/payment
and Django finds there
a) in the view a reverse(‘order_successful’) or similar
b) in the view’s html a tag {% url ‘order_successful’ % }
AND as there is no additional value given throughs the exception.
As your urls.py contains
path('product-details/payment', views.payment, name='payment'),
I would suggest to search in views.payment and in the rendered html (file is not clear here as you do not post views.payment).
- the second line of your error message says
tried: ['order_success/(?P[^/]+)$'] .
this is strange because with your current urls.py you do not have a path that would cause such an error message. With your urls.py it should be
tried: ['order_success/(?P<DATA>[0-9]+)$'] .
This is why I ask you to carefully check the files as with the current inconsistency error/related files it is not possible to point you to the a solution.
So agian: did you really post the actual versions of your files (urls.y …)?
Another hint: please search for ‘order_successful’ in all your html’s. if you have some old “comments” in there with {% url ‘order_successful’ %}, django will process that as url tag as long as you do not enclose it in the django specific comments markers