# django-namespaces
Use namespaces in requests using Django.
## Motivation
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).
## Installation
Use a virtual environment whenever using Python packages. The built-in [venv](https://docs.python.org/3/library/venv.html) module is great.
```
python3 -m venv venv
source venv/bin/activate
$(venv) python -m pip install django-namespaces --upgrade
```
## Configure your Django Project
### Create a Django Project
```
$(venv) mkdir -p src && cd src
$(venv) django-admin startproject cfehome .
```
### Installed Apps
Add `django_namespaces` to your `INSTALLED_APPS` in `settings.py`:
```python
INSTALLED_APPS = [
...
'django_namespaces',
]
```
### Update Middelware
Update `MIDDLEWARE` in `settings.py` to include `NamespaceMiddleware`:
```python
MIDDLEWARE = [
...
'django_namespaces.middleware.NamespaceMiddleware',
]
```
This gives us access to the `request.namespace` object in our views.
## Basic Usage
```python
import django_namespaces
django_namespaces.activate("hello-world")
```
This will add a namespace to the request object.
```python
def my_hello_world_view(request):
print(request.namespace) # <Namespace: hello-world>
print(request.namespace.value) # hello-world
return HttpResponse("Hello World")
```
### 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`:
```python
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
Raw data
{
"_id": null,
"home_page": "https://github.com/jmitchel3/django-namespaces",
"name": "django-namespaces",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "Django",
"author": "Justin Mitchel",
"author_email": "hello@teamcfe.com",
"download_url": "https://files.pythonhosted.org/packages/04/63/3543afbb69ef3d93d3f92dee4b3428f4829afc92a86d062ae171dd661b7f/django-namespaces-0.0.19.tar.gz",
"platform": null,
"description": "# django-namespaces\n\nUse namespaces in requests using Django.\n\n\n## Motivation\n\nGoogle 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).\n\n\n## Installation\n\nUse a virtual environment whenever using Python packages. The built-in [venv](https://docs.python.org/3/library/venv.html) module is great.\n```\npython3 -m venv venv\nsource venv/bin/activate\n$(venv) python -m pip install django-namespaces --upgrade\n```\n\n## Configure your Django Project\n\n### Create a Django Project\n```\n$(venv) mkdir -p src && cd src\n$(venv) django-admin startproject cfehome .\n```\n\n### Installed Apps\nAdd `django_namespaces` to your `INSTALLED_APPS` in `settings.py`:\n```python\nINSTALLED_APPS = [\n ...\n 'django_namespaces',\n]\n```\n\n### Update Middelware\nUpdate `MIDDLEWARE` in `settings.py` to include `NamespaceMiddleware`:\n```python\nMIDDLEWARE = [\n ...\n 'django_namespaces.middleware.NamespaceMiddleware',\n]\n```\n\n\nThis gives us access to the `request.namespace` object in our views.\n\n\n## Basic Usage\n\n```python\nimport django_namespaces\ndjango_namespaces.activate(\"hello-world\")\n```\nThis will add a namespace to the request object.\n\n```python\n\ndef my_hello_world_view(request):\n print(request.namespace) # <Namespace: hello-world>\n print(request.namespace.value) # hello-world\n return HttpResponse(\"Hello World\") \n```\n\n\n### Optional Views\nUsing views are optional. You can also use the `activate` function to activate a namespace.\n\n#### Update URLconf\nUpdate `urls.py` to include `namespaces.urls`:\n```python\nurlpatterns = [\n ...\n path('namespaces/', include('django_namespaces.urls')),\n]\n```\n\n\n#### Create a Namespace\nCreate a namespace by visiting `http://localhost:8000/namespaces/create/` and filling out the form.\n\n\n#### Activate a Namespace\nActivate a namespace by visiting `http://localhost:8000/namespaces/` and hitting `activate` on your newly created namespace.\n\nYou can also use:\n\n\n#### Update URLconf\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "",
"version": "0.0.19",
"split_keywords": [
"django"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "d97f72549b829edd0bba0e7ae7c3ac3ad9b742c6b3072883fc652d5fee49518e",
"md5": "c569da32152d282bbce483c900ba8dec",
"sha256": "a3b78f7d14111061615a9c28bec811aa31a77f0476c9f13d626ef2ac719f926e"
},
"downloads": -1,
"filename": "django_namespaces-0.0.19-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c569da32152d282bbce483c900ba8dec",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 20852,
"upload_time": "2023-03-17T19:47:24",
"upload_time_iso_8601": "2023-03-17T19:47:24.948081Z",
"url": "https://files.pythonhosted.org/packages/d9/7f/72549b829edd0bba0e7ae7c3ac3ad9b742c6b3072883fc652d5fee49518e/django_namespaces-0.0.19-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "04633543afbb69ef3d93d3f92dee4b3428f4829afc92a86d062ae171dd661b7f",
"md5": "7e95132eb75ec2edc5da779fe9c292f7",
"sha256": "e40a3d1109a2e591081755c0429cec4cfaeb61c6f166d42ef19141a807234dcc"
},
"downloads": -1,
"filename": "django-namespaces-0.0.19.tar.gz",
"has_sig": false,
"md5_digest": "7e95132eb75ec2edc5da779fe9c292f7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 15602,
"upload_time": "2023-03-17T19:47:26",
"upload_time_iso_8601": "2023-03-17T19:47:26.505653Z",
"url": "https://files.pythonhosted.org/packages/04/63/3543afbb69ef3d93d3f92dee4b3428f4829afc92a86d062ae171dd661b7f/django-namespaces-0.0.19.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-03-17 19:47:26",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "jmitchel3",
"github_project": "django-namespaces",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "django-namespaces"
}