Use namespaces in requests using Django.
Google Cloud has an interest feature for namespacing projects. Namespacing can enable assets to be isolated from each other without needing to leverage subdomains and/or multiple databases (although you can use those too).
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
$(venv) mkdir -p src && cd src
$(venv) django-admin startproject cfehome .
Add django_namespaces
to your INSTALLED_APPS
in settings.py
:
INSTALLED_APPS = [
...
'django_namespaces',
]
Update MIDDLEWARE
in settings.py
to include NamespaceMiddleware
:
MIDDLEWARE = [
...
'django_namespaces.middleware.NamespaceMiddleware',
]
This gives us access to the request.namespace
object in our views.
import django_namespaces
django_namespaces.activate("hello-world")
This will add a namespace to the request object.
def my_hello_world_view(request):
print(request.namespace) # <Namespace: hello-world>
print(request.namespace.value) # hello-world
return HttpResponse("Hello World")
Using views are optional. You can also use the activate
function to activate a namespace.
Update urls.py
to include namespaces.urls
:
urlpatterns = [
...
path('namespaces/', include('django_namespaces.urls')),
]
Create a namespace by visiting http://localhost:8000/namespaces/create/
and filling out the form.
Activate a namespace by visiting http://localhost:8000/namespaces/
and hitting activate
on your newly created namespace.
You can also use: