diff --git a/docs/examples/django.rst b/docs/examples/django.rst new file mode 100644 index 00000000..d44c055c --- /dev/null +++ b/docs/examples/django.rst @@ -0,0 +1,95 @@ +Django example +============== + +.. meta:: + :keywords: Python,Dependency Injection,Django,Example + :description: This example demonstrates a usage of the Django and Dependency Injector. + + +This example shows how to use ``Dependency Injector`` with ``Django``. + +The example application helps to search for repositories on the Github. + +.. image:: images/django.png + :width: 100% + :align: center + +The source code is available on the `Github `_. + +Application structure +--------------------- + +Application has standard Django project structure. It consists of ``githubnavigator`` project package and +``web`` application package: + +.. code-block:: bash + + ./ + ├── githubnavigator/ + │ ├── __init__.py + │ ├── asgi.py + │ ├── containers.py + │ ├── services.py + │ ├── settings.py + │ ├── urls.py + │ └── wsgi.py + ├── web/ + │ ├── templates/ + │ │ ├── base.html + │ │ └── index.html + │ ├── __init__.py + │ ├── apps.py + │ ├── tests.py + │ ├── urls.py + │ └── views.py + ├── manage.py + └── requirements.txt + +Container +--------- + +Declarative container is defined in ``githubnavigator/containers.py``: + +.. literalinclude:: ../../examples/miniapps/django/githubnavigator/containers.py + :language: python + +Container instance is created in ``githubnavigator/__init__.py``: + +.. literalinclude:: ../../examples/miniapps/django/githubnavigator/__init__.py + :language: python + +Views +----- + +View has dependencies on search service and some config options. The dependencies are injected +using :ref:`wiring` feature. + +Listing of ``web/views.py``: + +.. literalinclude:: ../../examples/miniapps/django/web/views.py + :language: python + +App config +---------- + +Container is wired to the ``views`` module in the app config ``web/apps.py``: + +.. literalinclude:: ../../examples/miniapps/django/web/apps.py + :language: python + :emphasize-lines: 12 + +Tests +----- + +Tests use :ref:`provider-overriding` feature to replace search service with a mock ``web/tests.py``: + +.. literalinclude:: ../../examples/miniapps/django/web/tests.py + :language: python + :emphasize-lines: 39,60 + +Sources +------- + +Explore the sources on the `Github `_. + +.. disqus:: diff --git a/docs/examples/images/django.png b/docs/examples/images/django.png new file mode 100644 index 00000000..2d399156 Binary files /dev/null and b/docs/examples/images/django.png differ diff --git a/docs/examples/index.rst b/docs/examples/index.rst index 17c62462..dcbb1639 100644 --- a/docs/examples/index.rst +++ b/docs/examples/index.rst @@ -13,5 +13,6 @@ Explore the examples to see the ``Dependency Injector`` in action. application-single-container application-multiple-containers decoupled-packages + django .. disqus::