skip to Main Content

I’m encountering a Not Found: /payment_getway/ error in my Django application. Despite setting up the URL path and view function, Django cannot find the specified URL. I’ve provided all relevant code snippets and configurations below. I’m not sure what might be causing this issue.

Here’s a brief overview of what I’ve done:

  1. URL Configuration:

In my app’s urls.py, I have defined the path for the payment_getway view function:

urlpatterns = [
    path('payment_getway/', payment_getway, name='payment_getway'),
    path('order-confirmation/', order_confirmation, name='order_confirmation'),
]
  1. View Function:

The view function payment_getway is defined as follows:

from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone
from .models import CartOrder, Address

def payment_getway(request):
    if request.method == "POST":
        user = request.user
        address_id = request.POST.get('address_id')  # Ensure this ID is passed in your form

        # Retrieve the address instance
        address_instance = get_object_or_404(Address, id=address_id, user=user)

        # Create a new CartOrder instance
        order = CartOrder(
            user=user,
            full_name=address_instance.name,
            email=address_instance.email,
            mobile=address_instance.mobile,
            address=address_instance,  # Assign the Address instance here
            landmark=address_instance.landmark,
            city=address_instance.region,
            state=address_instance.region,  # Update as per your address model
            postalCode=address_instance.pin,
            item="Your Item",  # Update this as necessary
            price=100.00,  # Example price, adjust accordingly
            saved=0.00,  # Example saved amount, adjust accordingly
            shipping_method="Standard",  # Update this as necessary
            tracking_id="Tracking ID",  # Example tracking ID
            tracking_website="Tracking Website",  # Example tracking website
            paid_status=False,
            stripe_payment_intent="Stripe Intent",  # Example Stripe Intent
            order_date=timezone.now(),  # Use timezone.now() for current datetime
            product_status="processing",  # Example status
        )
        order.save()

        return redirect('core:order_confirmation')

    user = request.user
    addresses = Address.objects.filter(user=user)
    return render(request, "core/payment-getway.html", {"addresses": addresses})
  1. Template:

The payment-getway.html template is designed as follows:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Payment Options</title>
    <!-- Font Awesome CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f0f2f5;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            width: 100%;
            max-width: 600px;
            margin: 0 auto;
            padding: 20px;
        }

        .card {
            background: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            padding: 20px;
            max-width: 500px;
            margin: 0 auto;
        }

        header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
        }

        .header-left {
            display: flex;
            align-items: center;
        }

        .back-btn {
            text-decoration: none;
            font-size: 20px;
            margin-right: 10px;
        }

        .step-info {
            font-size: 14px;
            color: #888;
        }

        .secure {
            font-size: 14px;
            color: #888;
        }

        h1 {
            font-size: 24px;
            margin-bottom: 10px;
        }

        .total-amount {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px;
            background-color: #f0f4ff;
            margin-bottom: 10px;
            border-radius: 5px;
        }

        .amount {
            font-size: 20px;
            font-weight: bold;
        }

        .discount {
            padding: 10px;
            background-color: #e0f9e6;
            margin-bottom: 20px;
            border-radius: 5px;
        }

        .discount span:first-child {
            font-size: 16px;
            font-weight: bold;
        }

        .address-box {
            padding: 15px;
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            margin-bottom: 20px;
        }

        .address-box h2 {
            font-size: 18px;
            margin-bottom: 10px;
        }

        .address-box p {
            font-size: 14px;
            color: #666;
            margin: 5px 0;
        }

        .payment-options {
            border-top: 1px solid #ccc;
        }

        .option {
            border-bottom: 1px solid #ccc;
            padding: 10px 0;
            cursor: pointer;
        }

        .option-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 16px;
            font-weight: bold;
        }

        .arrow {
            transition: transform 0.3s ease;
        }

        .arrow.down {
            transform: rotate(180deg);
        }

        .option-body {
            display: none;
            font-size: 14px;
            color: #666;
            margin-top: 5px;
        }

        .option-body span {
            color: green;
        }

        .option-body a {
            color: blue;
            text-decoration: none;
        }

        .place-order-btn {
            display: block;
            margin-top: 10px;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
            width: 100%;
            text-align: center;
            border-radius: 5px;
        }

        .hidden {
            display: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="card">
            <header>
                <div class="header-left">
                    <a href="#" class="back-btn">←</a>
                    <span class="step-info">Go Back</span>
                </div>
                <div class="header-right">
                    <span class="secure">🔒 100% Secure</span>
                </div>
            </header>
            
            <main>
                <h1>Payments</h1>
                <div class="total-amount">
                    <span class="amount">₹ 35,990</span>
                </div>
                <div class="discount">
                    <span>5% instant discount</span>
                    <span>Claim now with payment offers</span>
                </div>
                
                <div class="address-box">
                    <h2>Delivery Address</h2>
                    <p><strong>{{ address.name }}</strong></p>
                    <p>{{ address.address }}</p>
                    <p>{{ address.region }} - {{ address.pin }}</p>
                    <p>{{ address.mobile }}</p>
                </div>

                <form method="post">
                    {% csrf_token %}
                    <input type="hidden" id="selected-payment-option" name="payment_option" value="">
                    
                    <div class="payment-options">
                        <div class="option" data-option="saved">
                            <div class="option-header">
                                Saved options
                                <i class="fas fa-chevron-down arrow"></i>
                            </div>
                            <div class="option-body">
                                <button type="submit" class="place-order-btn" data-option="saved">Place Order</button>
                            </div>
                        </div>
                        <div class="option" data-option="upi">
                            <div class="option-header">
                                UPI
                                <i class="fas fa-chevron-down arrow"></i>
                            </div>
                            <div class="option-body">
                                Pay by any UPI app
                                <button type="submit" class="place-order-btn" data-option="upi">Place Order</button>
                            </div>
                        </div>
                        <div class="option" data-option="card">
                            <div class="option-header">
                                Credit / Debit / ATM Card
                                <i class="fas fa-chevron-down arrow"></i>
                            </div>
                            <div class="option-body">
                                Add and secure cards as per RBI guidelines<br>
                                <span>Save upto ₹1,500 • 2 offers available</span>
                                <button type="submit" class="place-order-btn" data-option="card">Place Order</button>
                            </div>
                        </div>
                        <div class="option" data-option="emi">
                            <div class="option-header">
                                EMI
                                <i class="fas fa-chevron-down arrow"></i>
                            </div>
                            <div class="option-body">
                                Get Debit and Cardless EMIs on HDFC Bank
                                <button type="submit" class="place-order-btn" data-option="emi">Place Order</button>
                            </div>
                        </div>
                        <div class="option" data-option="netbanking">
                            <div class="option-header">
                                Net Banking
                                <i class="fas fa-chevron-down arrow"></i>
                            </div>
                            <div class="option-body">
                                <button type="submit" class="place-order-btn" data-option="netbanking">Place Order</button>
                            </div>
                        </div>
                        <div class="option" data-option="wallets">
                            <div class="option-header">
                                Wallets
                                <i class="fas fa-chevron-down arrow"></i>
                            </div>
                            <div class="option-body">
                                <button type="submit" class="place-order-btn" data-option="wallets">Place Order</button>
                            </div>
                        </div>
                        <div class="option" data-option="cod">
                            <div class="option-header">
                                Cash on Delivery
                                <i class="fas fa-chevron-down arrow"></i>
                            </div>
                            <div class="option-body">
                                <button type="submit" class="place-order-btn" data-option="cod">Place Order</button>
                            </div>
                        </div>
                        <div class="option" data-option="coupon">
                            <div class="option-header">
                                Have a Coupon Card?
                                <i class="fas fa-chevron-down arrow"></i>
                            </div>
                            <div class="option-body">
                                <a href="#">Add</a>
                                <button type="submit" class="place-order-btn" data-option="coupon">Place Order</button>
                            </div>
                        </div>
                    </div>
                </form>
            </main> 
        </div>
    </div>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            const options = document.querySelectorAll('.option');
            const selectedOptionInput = document.getElementById('selected-payment-option');

            options.forEach(option => {
                const header = option.querySelector('.option-header');
                const arrow = header.querySelector('.arrow');
                const body = option.querySelector('.option-body');
                const button = option.querySelector('.place-order-btn');

                header.addEventListener('click', function () {
                    const isVisible = body.style.display === 'block';
                    document.querySelectorAll('.option-body').forEach(body => body.style.display = 'none');
                    document.querySelectorAll('.arrow').forEach(arrow => arrow.classList.remove('down'));

                    if (isVisible) {
                        body.style.display = 'none';
                    } else {
                        body.style.display = 'block';
                        arrow.classList.add('down');
                    }
                });

                button.addEventListener('click', function () {
                    const paymentOption = option.getAttribute('data-option');
                    selectedOptionInput.value = paymentOption;
                });
            });
        });
    </script>
</body>
</html>

Troubleshooting Steps Taken:

  1. Verified the URL pattern in urls.py.
  2. Checked the view function in views.py for errors.
  3. Confirmed that the template payment-getway.html exists and is in the correct directory.
  4. Restarted the Django development server.

Questions:

  1. Why is Django not finding the /payment_getway/ URL?
  2. Is there a potential issue with how the URL is defined or how the view function is set up?
  3. Are there any additional checks I should perform to ensure that the URL routing is configured correctly?

Any guidance or suggestions would be greatly appreciated!

Thank you!

2

Answers


  1. You did not map your URLs to your View functions. In your urls.py, first import your views

    from . import views
    

    Then map your URLs to your views:

    urlpatterns = [
        path('payment_getway/', views.payment_getway, name='payment_getway'),
        path('order-confirmation/', views.order_confirmation, name='order_confirmation'),
    ]
    

    See URL dispatcher

    Login or Signup to reply.
  2. 안녕하세요. 나는 한국의 장고 이용자입니다.

    I think you didn’t add the url to the default urls.py.
    urls.py that in several app directory need add on config/urls.py

    # config/urls.py
    
    urlpatterns = [
      ...
      path('payment_getway/', payment_getway, name='payment_getway'),
      path('order-confirmation/', order_confirmation, name='order_confirmation'),
    ]
    
    OR
    
    from django.urls import include
    urlpatterns = [
      ...
      path({url path}, include({app name.urls})),
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search