Skip to main content

Add namespaces to your Django requests. Helps with project isolation.

Project description

django-namespaces

Use namespaces in requests using Django.

Motivation

With django-namespaces, we get a new way to group resources based on request.namespace.

Installation

Use a virtual environment whenever using Python packages. The built-in venv module is great.

python3 -m venv venv
source venv/bin/activate
$(venv) python -m pip install django-namespaces --upgrade

Django Settings (settings.py)

Installed Apps

Add django_namespaces to INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_namespaces",
]

Middleware

Update MIDDLEWARE:

MIDDLEWARE = [
    # ...
    "django_namespaces.middleware.NamespaceMiddleware",
]

This gives us access to the request.namespace object in our views.

Template Context Processors

Add django_namespaces.context_processors.user_namespaces to TEMPLATE_CONTEXT_PROCESSORS:

TEMPLATE_CONTEXT_PROCESSORS = [
    # ...
    "django_namespaces.context_processors.user_namespaces",
]

Usage Usage

from django.contrib.auth import get_user_model
from django_namespaces.models import Namespace

User = get_user_model()
user = User.objects.create_user(username="jon.snow", password="youknowsomething")

namespace = Namespace.objects.create(handle="winterfell", user=user)
namespace2 = Namespace.objects.create(handle="thewall", user=user)
import django_namespaces

django_namespaces.activate("winterfell")

This will add a namespace to the request object.

def my_hello_world_view(request):
    print(request.namespace)  # <Namespace: winterfell>
    print(request.namespace.handle)  # winterfell
    return HttpResponse("Hello World")
from django.db import models
from django_namespaces.models import Namespace


class Location(models.Model):
    name = models.CharField(max_length=255, blank=True, null=True)
    namespace = models.ForeignKey(Namespace, on_delete=models.CASCADE)

Optional Views

Using views are optional. You can also use the activate function to activate a namespace.

Update URLconf

Update urls.py to include namespaces.urls:

urlpatterns = [
    # ...
    path("namespaces/", include("django_namespaces.urls")),
]

Create a Namespace

Create a namespace by visiting http://localhost:8000/namespaces/create/ and filling out the form.

Activate a Namespace

Activate a namespace by visiting http://localhost:8000/namespaces/ and hitting activate on your newly created namespace.

You can also use:

Update URLconf

Project details


Download files

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

Source Distribution

django_namespaces-0.0.21.tar.gz (33.7 kB view details)

Uploaded Source

Built Distribution

django_namespaces-0.0.21-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file django_namespaces-0.0.21.tar.gz.

File metadata

  • Download URL: django_namespaces-0.0.21.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for django_namespaces-0.0.21.tar.gz
Algorithm Hash digest
SHA256 2a67c5660da2d91770ffb88b0fb0cb76c8de2a75aa22564827fa5dc1e21ebb91
MD5 873291a9799c34da4abb8c82c19cfbd8
BLAKE2b-256 d99912e19967db273e7af39c31e042fd74fae9e4eedba48073cfd25e6fc2ad7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_namespaces-0.0.21.tar.gz:

Publisher: main.yaml on jmitchel3/django-namespaces

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_namespaces-0.0.21-py3-none-any.whl.

File metadata

File hashes

Hashes for django_namespaces-0.0.21-py3-none-any.whl
Algorithm Hash digest
SHA256 9304e3ac9432416a7986bd099665d3c7d5fdbe786766c4973dfe37e8146aaf27
MD5 e88a40e588fa132dfb92fa40fd592d87
BLAKE2b-256 5a61a89c5a150471d58e6cfc3908ff9611caf4d4e725d3b8e13f4d0e1da39339

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_namespaces-0.0.21-py3-none-any.whl:

Publisher: main.yaml on jmitchel3/django-namespaces

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page