Skip to main content

Coupons — Django Coupon & Discount Code System

PyPI version Python Django License: MIT

Author: Nitesh Kumar Singh · GitHub: @nkscoder

A reusable Django coupons app for managing promotional codes, discount rules, and coupon validation in Python web applications. Built by Nitesh Kumar Singh (nkscoder) for e-commerce, SaaS, and any Django project that needs flexible coupon functionality.

Keywords: django coupons · python coupon system · discount code · promo code · coupon validation · nkscoder · nitesh kumar singh


Features

  • Percentage or fixed-amount discounts on any order total
  • User-specific or global coupons — restrict codes to selected users or allow all
  • Usage limits — max total uses, uses per user, or unlimited
  • Expiration & active/inactive rules with datetime-based validity
  • Django Admin integration with bulk actions (reset usage, delete expired)
  • Simple validation API — one function call to check any coupon code
  • Configurable coupon code length via DSC_COUPON_CODE_LENGTH setting

Requirements

  • Python 3.8+
  • Django 3.2+

Installation

pip install coupons

From GitHub

pip install git+https://github.com/nkscoder/coupons.git

Setup

Step 1 — Add to INSTALLED_APPS

# settings.py
INSTALLED_APPS = [
    ...
    "coupons",
]

Step 2 — Run migrations

python manage.py migrate coupons

Step 3 — Create coupons in Django Admin

Go to /admin/ and create:

  1. Discount — set value and type (percentage or fixed)
  2. Allowed Users Rule — select users or enable "All users"
  3. Max Uses Rule — set limits or enable infinite uses
  4. Validity Rule — set expiration date and active status
  5. Ruleset — link the three rules above
  6. Coupon — assign discount and ruleset (code auto-generated)

Step 4 (optional) — Custom coupon code length

# settings.py
DSC_COUPON_CODE_LENGTH = 16  # default is 12

Usage

Validate a coupon

from coupons.validations import validate_coupon

coupon_code = "COUPONTEST01"
user = User.objects.get(username="nitesh")

status = validate_coupon(coupon_code=coupon_code, user=user)
# {'valid': True}

if status["valid"]:
    print("Coupon is valid!")
else:
    print(status["message"])  # e.g. "Coupon does not exist!"

Apply a coupon (record usage)

from coupons.models import Coupon

coupon = Coupon.objects.get(code=coupon_code)
coupon.use_coupon(user=user)

Get discount details

coupon = Coupon.objects.get(code=coupon_code)
discount = coupon.get_discount()
# {'value': 50, 'is_percentage': True}

Calculate discounted price

discounted = coupon.get_discounted_value(initial_value=100.0)
# Returns 50.0 for 50% off, or 80.0 for $20 off

Validation Rules

Rule Description
Allowed Users Coupon valid only for selected users, or all users
Max Uses Global usage cap, per-user limit, or infinite
Validity Active flag + expiration datetime

Invalid responses include a human-readable message key:

validate_coupon("DUMMYCODE", user)
# {'valid': False, 'message': 'Coupon does not exist!'}

REST API Example

See examples/ for a Django REST Framework integration sample (ajax_views.py, ajax_urls.py).


Development

git clone git@github.com:nkscoder/coupons.git
cd coupons
pip install -e ".[dev]"
python manage.py migrate
python manage.py runserver

Build & publish to PyPI

pip install build twine
python -m build
twine upload dist/*

Or tag a release on GitHub — the included GitHub Action publishes automatically.


Changelog

1.1.0

  • Modernized for Django 3.2–5.x and Python 3.8+
  • Added PyPI packaging (pyproject.toml)
  • Fixed per-coupon usage validation bug
  • Fixed template mutation in validation responses
  • SEO & documentation update by Nitesh Kumar Singh (nkscoder)

1.0.0

  • Initial release

Author Nitesh Kumar Singh
GitHub github.com/nkscoder
Repository github.com/nkscoder/coupons
PyPI pypi.org/project/coupons

License

MIT License — Copyright (c) 2020-2026 Nitesh Kumar Singh (nkscoder)

Metadata

Release files for coupons 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for coupons 1.1.2
File Size Uploaded Provenance Actions
coupons-1.1.2.tar.gz 11.2 kB Without provenance Details for coupons-1.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for coupons 1.1.2
File Interpreter ABI Platform Actions
coupons-1.1.2-py3-none-any.whl Python 3 none any Details for coupons-1.1.2-py3-none-any.whl

Total release size: 21.9 kB

Release files / coupons-1.1.2.tar.gz

Download URL coupons-1.1.2.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
cb43cb94c0ca9e2c0028a4e7791724bbdfee4043986277bcc8622b2e31ddb5b1
BLAKE2b-256 checksum
How to use checksums
bb93c644fe631baf9d7f81f3b82061fe484f80258160778b95fc3c156040b2b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Release files / coupons-1.1.2-py3-none-any.whl

Download URL coupons-1.1.2-py3-none-any.whl
Size 10.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2677dc87d35e50c45e78a54942f80f59f540580404ee9d13e78e62862b1cd51
BLAKE2b-256 checksum
How to use checksums
05e907a3465222c70161f7ac67e1ff530f00b554a0149fa53165902986479cf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.12

Security for coupons 1.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page