mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 18:07:44 +03:00 
			
		
		
		
	Add django example to the docs
This commit is contained in:
		
							parent
							
								
									361858950f
								
							
						
					
					
						commit
						71296092a6
					
				
							
								
								
									
										95
									
								
								docs/examples/django.rst
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								docs/examples/django.rst
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -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 <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/django>`_.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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 <https://github.com/ets-labs/python-dependency-injector/tree/master/examples/miniapps/django>`_.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.. disqus::
 | 
				
			||||||
							
								
								
									
										
											BIN
										
									
								
								docs/examples/images/django.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								docs/examples/images/django.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 382 KiB  | 
| 
						 | 
					@ -13,5 +13,6 @@ Explore the examples to see the ``Dependency Injector`` in action.
 | 
				
			||||||
    application-single-container
 | 
					    application-single-container
 | 
				
			||||||
    application-multiple-containers
 | 
					    application-multiple-containers
 | 
				
			||||||
    decoupled-packages
 | 
					    decoupled-packages
 | 
				
			||||||
 | 
					    django
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. disqus::
 | 
					.. disqus::
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user