Skip to content

ettoreleandrotognoli/django-pycdi

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Django + PyCDI

https://travis-ci.org/ettoreleandrotognoli/django-pycdi.svg?branch=master

A middleware to integrate PyCDI with Django.

See the code of conduct.

Install

Install pypi version

pip install django_pycdi

Install lastest version

pip install git+https://github.com/ettoreleandrotognoli/django-pycdi.git

Usage

Add middleware to settings.py:

MIDDLEWARE += ['django_pycdi.middlewares.CDIMiddleware']

Add inject decorator to your views:

With Python 2:

from random import random
from django.http import HttpResponse
from pycdi import Inject, Producer
from pycdi.utils import Singleton


@Singleton()
class MySingleton():
    pass

@Producer(float)
def get_a_float():
    return random()

@Inject(singleton=MySingleton,number=float)
def view(request,singleton,number):
    return HttpResponse('...')

In the demo3 folder has a django demo project using python2 with more examples.

With Python 3:

from random import random
from django.http import HttpResponse
from pycdi import Inject, Producer
from pycdi.utils import Singleton

@Singleton()
class MySingleton():
    pass

@Producer()
def get_a_float() -> float:
    return random()

@Inject()
def view(request,singleton:MySingleton,number:float):
    return HttpResponse('...')

In the demo3 folder has a django demo project using python3 with more examples.

See more ways to use PyCDI in its page.