Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
How to create if statement on a redirect to change Django model

I am trying to create a if statement in my Django view that detects when I am redirected to my complete order url. I want to do this because I would like to change my Django model Order 'complete' field to true. My redirect is coming from a javascript function in my paypal intergration.

checkout.html

    {% extends 'base.html' %}
{% load crispy_forms_tags %}



{% block content %}
<h1>Checkout</h1>
<div class='container'>
    <div class='row'>
        <div class='col-6'>
            <form action="" method='post' id='payement-form'>
                {% csrf_token %}
                <!-- {{ form|crispy }} -->
                <div class='col-12' id='paypal-button-container'></div>
            </form>
            <script
                src="https://www.paypal.com/sdk/js?client-id="> // Required. Replace YOUR_CLIENT_ID with your sandbox client ID.
                </script>

            <script>

                function redirect() {
                    var url = "{% url 'complete-order' %}"

                    window.location.href = url

                }
                paypal.Buttons({
                    createOrder: function (data, actions) {
                        return actions.order.create({
                            purchase_units: [{
                                amount: {
                                    value: '0.01'

                                }
                            }]
                        });
                    },
                    onApprove: function (data, actions) {
                        // This function captures the funds from the transaction.
                        return actions.order.capture().then(function (details) {
                            // This function is the redirect
                            redirect()
                            alert('Transaction completed by ' + details.payer.name.given_name);
                        });
                    }
                }).render('#paypal-button-container');
            </script>


        </div>

{% endblock content %}

views.py

    @login_required(login_url='login')
def checkout(request):
    order = Order.objects.get(user=request.user, complete=False)
    context = {
        'order': order
    }
    return render(request, 'videogames/checkout.html', context)


@login_required(login_url='login')
def paymentComplete(request):
    order = Order.objects.get(user=request.user, complete=True)
    context = {
        'order': order
    }
    return render(request, 'videogames/complete.html', context)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

There are many ways that should be selected according to your project.

  1. before redirection, in the view , call a link with Ajax to change the status
@login_required(login_url='login')
def paymentSetComplete(request):
        order = Order.objects.get(id=request.payment_id=,user=request.user, complete=False)
        order.complete=True
        order.save() # or update directly
        return 
  1. Change the status when the request to load the complate page was sent to django

I do not know what measures you have taken to secure and prevent the fake purchases But know that users can repeat requests that come to the server

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!