so I am using a payment that has a view called verify , and It is going to check if the result was Ok , do something . But I need to pass order_id in the previous view that is token from the url to this view . some suggestion was using the global . but I am afraid of a error in the real server .
If you need . Its the code :
Previous view that sends a request to the payment center :
def send_request(request , order_id):
i = order_id
amount = get_object_or_404(Order , id = i , user_id = request.user.id)
result = client.service.PaymentRequest(MERCHANT , amount.total , description , email , mobile , CallbackURL)
if result.Status == 100 :
return redirect('https://www.zarinpal.com/pg/StarPay' str(result.Authority))
else :
return HttpResponse('error code : ' , str(result.Status))
And I need the order_id in the next view
SO what can i do??Help please!
CodePudding user response:
To pass data from one view to another can be done in several way in Django.
You can use url parameter :
def present_view(request):
if condition:
return reverse('next_view', args(order_id))
return render(request, 'present_view.html', locals())
def next_view(request, order_id):
# Retrieve order_id from url parameter
id = order_id
# Do some stuff with id here
urls.py
path('present_view/', views.present_view, name=present_view),
path('next_view/<int:order_id>/', views.next_view, name=next_view)
You can use session :
def present_view(request):
if condition:
# Set the id in the session
request.session['order_id'] = 'some_id_123'
return reverse('next_view', args(order_id))
return render(request, 'present_view.html', locals())
def next_view(request, order_id):
# Retrieve order_id from session, id = 0 if order_id is not found
id = request.session.get('order_id', 0)
# Do some stuff with id here
urls.py
path('present_view/', views.present_view, name=present_view),
path('next_view/', views.next_view, name=next_view)