Giter Site home page Giter Site logo

django-namespaces's Introduction

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 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:

INSTALLED_APPS = [
    ...
    'django_namespaces',
]

Update Middelware

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.

Basic Usage

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") 

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

django-namespaces's People

Contributors

jmitchel3 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.