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

From PyPI (recommended)

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 & Links

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)

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

coupons-1.1.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

coupons-1.1.2-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file coupons-1.1.2.tar.gz.

File metadata

  • Download URL: coupons-1.1.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.12

File hashes

Hashes for coupons-1.1.2.tar.gz
Algorithm Hash digest
SHA256 cb43cb94c0ca9e2c0028a4e7791724bbdfee4043986277bcc8622b2e31ddb5b1
MD5 9ec500b954a0d5db629dc6e2214f68bf
BLAKE2b-256 bb93c644fe631baf9d7f81f3b82061fe484f80258160778b95fc3c156040b2b4

See more details on using hashes here.

File details

Details for the file coupons-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: coupons-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.12

File hashes

Hashes for coupons-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b2677dc87d35e50c45e78a54942f80f59f540580404ee9d13e78e62862b1cd51
MD5 35368e25753d495d477b97da547bb051
BLAKE2b-256 05e907a3465222c70161f7ac67e1ff530f00b554a0149fa53165902986479cf9

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 files

1.1.1

2 files

1.1.0

2 files

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