Django Paypal Simple Page

I’m trying to follow the Paypal tutorial site to build a simple working Paypal webpage to test with my sandbox accounts. The webpage won’t load and I have the webserver running. My other webpages work fine for my other apps.


(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal# cat urls.py
from django.urls import path, include
from . import views
from .views import Checkout

urlpatterns = [
    path('paypal/', include("paypal.standard.ipn.urls")),
    path('payment.html', views.Checkout, name='checkout'),
]
(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal# cat views.py
from django.shortcuts import render
from django.urls import reverse
from paypal.standard.forms import PayPalPaymentsForm
from django.conf import settings

def Checkout(request):

    # What you want the button to do.
    paypal_dict = {
        "business": "receiver_email@example.com",
        "amount": "10000000.00",
        "item_name": "name of the item",
        "invoice": "unique-invoice-id",
        "notify_url": request.build_absolute_uri(reverse('paypal-ipn')),
        "return": request.build_absolute_uri(reverse('your-return-view')),
        "cancel_return": request.build_absolute_uri(reverse('your-cancel-view')),
        "custom": "premium_plan",  # Custom command to correlate to some function later (optional)
    }

    # Create the instance.
    form = PayPalPaymentsForm(initial=paypal_dict)
    context = {"form": form}
    return render(request, "payment.html", context)
(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal# cat admin.py
from django.contrib import admin

# Register your models here.
(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal# cat hooks.py
from paypal.standard.models import ST_PP_COMPLETED
from paypal.standard.ipn.signals import valid_ipn_received

def show_me_the_money(sender, **kwargs):
    ipn_obj = sender
    if ipn_obj.payment_status == ST_PP_COMPLETED:
        # WARNING !
        # Check that the receiver email is the same we previously
        # set on the `business` field. (The user could tamper with
        # that fields on the payment form before it goes to PayPal)
        if ipn_obj.receiver_email != "receiver_email@example.com":
            # Not a valid payment
            return

        # ALSO: for the same reason, you need to check the amount
        # received, `custom` etc. are all what you expect or what
        # is allowed.

        # Undertake some action depending upon `ipn_obj`.
        if ipn_obj.custom == "premium_plan":
            price = ...
        else:
            price = ...

        if ipn_obj.mc_gross == price and ipn_obj.mc_currency == 'USD':
            ...
    else:
        #...

valid_ipn_received.connect(show_me_the_money)
(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal# cat templates/payment.html
<!DOCTYPE html>
<html>
<head>
    <title>Hello, World!</title>
</head>
<body>
    <h1>Hello, World!</h1>
</body>
</html>
(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal# cd templates/
(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal/templates# ls
payment.html
(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal/templates# cat ../../config/urls.py
"""
URL configuration for config project.

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/5.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from web import views as views

urlpatterns = [
    path("polls/", include("polls.urls")),
    path('', include('registration.urls')),
    path('registration/', include('registration.urls')),
    path('admin/', admin.site.urls),
    path('', include('paypal.standard.ipn.urls')),
    path('djangopaypal/', include('djangopaypal.urls')),
    #path('', views.index),
]

(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal/templates#

You forgot the most important thing: The error message or traceback you are getting.

There is a little thing (which is not really the problem in your case).

You imported Checkout in ~/cquence/djangopaypal/urls.py
from .views import Checkout

You did not need to import the entire views module
from . import views

remove the first import statement and adjust the relevant entry in urlpatterns to:
path('payment.html', Checkout, name='checkout'),

Note: It is conventional to use small letter case for functions – def checkout
You capitalize first letter when defining classes – class CheckoutView

This makes your code easier to read.
Finally, please share the error message or traceback you are getting

I really appreciate the feedback. I get a 500 server error when I go to the webpage. Syslog shows this

2024-08-09T05:42:41.720559+00:00 ubuntu-s-1vcpu-512mb-10gb-sfo3-01 gunicorn[1089815]: - - [08/Aug/2024:22:42:41 -0700] “GET / HTTP/1.0” 405 0 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”
2024-08-09T05:43:16.353337+00:00 ubuntu-s-1vcpu-512mb-10gb-sfo3-01 gunicorn[1089815]: - - [08/Aug/2024:22:43:16 -0700] “GET /djangopaypal/payment.html HTTP/1.0” 500 145 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”

I did adjust urls.py so it looks like this now

(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/djangopaypal# cat urls.py
from django.urls import path, include
#from . import views
from .views import Checkout

urlpatterns = [
path(‘paypal/’, include(“paypal.standard.ipn.urls”)),
path(‘payment.html’, Checkout, name=‘checkout’),
]

This is a deployed instance. Does the page load in development outside a webserver?

For instance, what happens when you run:
./manage.py runserver

then access the page on a development/local machine:
http://localhost:8000/djangopaypal/payment.html

Any error messages?

This is running in digital ocean. I have it running in the background with gunicorn so the webserver is always up, I just restart gunicorn whenever I make changes to test. I have other pages under different apps that come up fine. For what ever reason this simple payment.html won’t come up.

I thought maybe it was my main urls.py but it looks correct to me

(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/config# cat urls.py
“”"
URL configuration for config project.

The urlpatterns list routes URLs to views. For more information please see:
URL dispatcher | Django documentation | Django
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path(‘’, views.home, name=‘home’)
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path(‘’, Home.as_view(), name=‘home’)
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path(‘blog/’, include(‘blog.urls’))
“”"
from django.contrib import admin
from django.urls import include, path
from web import views as views

urlpatterns = [
path(“polls/”, include(“polls.urls”)),
path(‘’, include(‘registration.urls’)),
path(‘registration/’, include(‘registration.urls’)),
path(‘admin/’, admin.site.urls),
path(‘’, include(‘paypal.standard.ipn.urls’)),
path(‘djangopaypal/’, include(‘djangopaypal.urls’)),
#path(‘’, views.index),
]

Same 500 error messages
2024-08-09T05:51:28.440023+00:00 ubuntu-s-1vcpu-512mb-10gb-sfo3-01 gunicorn[1112109]: - - [08/Aug/2024:22:51:28 -0700] “GET /djangopaypal/payment.html HTTP/1.0” 500 145 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”
2024-08-09T05:51:28.612688+00:00 ubuntu-s-1vcpu-512mb-10gb-sfo3-01 gunicorn[1112108]: - - [08/Aug/2024:22:51:28 -0700] “GET /djangopaypal/payment.html HTTP/1.0” 500 145 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”

I’m not really sure how to run it in a browser on a droplet and the run server is running in the background constantly since I already have other live pages up and running.

Which server are you running? You need to share the error logs for the webserver. Also, remember to format your posts. Your code should be wrapped between ``` quotes so that the platform displays them correctly

This is from the syslog. I’m running the lowest level linux droplet ubuntu. What other logs can I provide?

2024-08-09T05:57:14.081420+00:00 ubuntu-s-1vcpu-512mb-10gb-sfo3-01 gunicorn[1112109]: - - [08/Aug/2024:22:57:14 -0700] “GET /payment.html HTTP/1.0” 404 179 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”
2024-08-09T05:58:55.179091+00:00 ubuntu-s-1vcpu-512mb-10gb-sfo3-01 gunicorn[1112107]: - - [08/Aug/2024:22:58:55 -0700] “GET /spring_2024.html? HTTP/1.0” 200 2441 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”
2024-08-09T05:58:56.678949+00:00 ubuntu-s-1vcpu-512mb-10gb-sfo3-01 gunicorn[1112109]: - - [08/Aug/2024:22:58:56 -0700] “GET /spring_2024.html? HTTP/1.0” 200 2441 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”

The access.log isn’t showing me much either

[09/Aug/2024:05:51:26 +0000] “GET /djangopaypal/payment.html HTTP/1.1” 500 145 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”
[09/Aug/2024:05:51:28 +0000] “GET /djangopaypal/payment.html HTTP/1.1” 500 145 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”
[09/Aug/2024:05:51:28 +0000] “GET /djangopaypal/payment.html HTTP/1.1” 500 145 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”
[09/Aug/2024:05:51:28 +0000] “GET /djangopaypal/payment.html HTTP/1.1” 500 145 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”
[09/Aug/2024:05:51:28 +0000] “GET /djangopaypal/payment.html HTTP/1.1” 500 145 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”
[09/Aug/2024:05:55:13 +0000] “GET / HTTP/1.1” 405 0 “-” “-”
[09/Aug/2024:05:57:04 +0000] “GET /djangopaypal/payment.html HTTP/1.1” 500 145 “-” “Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36”

For nginx there is /var/log/nginx/error.log
You might want to run your django project on a local machine first using DEBUG=True and ./manage.py runserver to figure out why the page is not loading

You are getting a 404 message on that request. Django is not finding the payment page

1 Like

the 404 is because I tried the URL without djangopaypal in the path to see if it would help so I think it should have it in there.

Not a whole lot in there

(venv) root@ubuntu-s-1vcpu-512mb-10gb-sfo3-01:~/cquence/registration# cat /var/log/nginx/error.log
2024/08/09 00:53:38 [error] 813743#813743: *14899 open() “/root/cquence/static/admin/css/base.css” failed (13: Permission denied), client: , server: , request: “GET /static/admin/css/base.css HTTP/1.1”, host: “”, referrer: “http:///admin/login/?next=/admin/”
2024/08/09 00:53:38 [error] 813743#813743: *14902 open() “/root/cquence/static/admin/css/dark_mode.css” failed (13: Permission denied), client: , server: , request: “GET /static/admin/css/dark_mode.css HTTP/1.1”, host: “”, referrer: “http:///admin/login/?next=/admin/”
2024/08/09 00:53:38 [error] 813743#813743: *14899 open() “/root/cquence/static/admin/css/nav_sidebar.css” failed (13: Permission denied), client: , server: , request: “GET /static/admin/css/nav_sidebar.css HTTP/1.1”, host: “”, referrer: “http:///admin/login/?next=/admin/”
2024/08/09 00:53:38 [error] 813743#813743: *14903 open() “/root/cquence/static/admin/css/login.css” failed (13: Permission denied), client: , server: , request: “GET /static/admin/css/login.css HTTP/1.1”, host: “”, referrer: “http:///admin/login/?next=/admin/”
2024/08/09 00:53:38 [error] 813743#813743: *14904 open() “/root/cquence/static/admin/css/responsive.css” failed (13: Permission denied), client: , server: , request: “GET /static/admin/css/responsive.css HTTP/1.1”, host: “”, referrer: “http:///admin/login/?next=/admin/”
2024/08/09 00:53:38 [error] 813743#813743: *14904 open() “/root/cquence/static/admin/js/theme.js” failed (13: Permission denied), client: , server: , request: “GET /static/admin/js/theme.js HTTP/1.1”, host: “”, referrer: “http:///admin/login/?next=/admin/”
2024/08/09 00:53:38 [error] 813743#813743: *14903 open() “/root/cquence/static/admin/js/nav_sidebar.js” failed (13: Permission denied), client: , server: , request: “GET /static/admin/js/nav_sidebar.js HTTP/1.1”, host: “”, referrer: “http:///admin/login/?next=/admin/”