Context Processors with Django

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] in this lesson you'll learn about Django context processors and I prepare the project with a common problem scenario which I'll use to use trade the practical use of context processors let's start I have here a bookstore project in navigation menu here all available book categories are displayed administrator user can add remove or edit any category and changes will be immediately reflected in this menu let me show that so for example if I add a category programming and category marketing in this side you see marketing and programming appear this dynamic categories menu is the central theme of this project its implementation is pretty simple I have here a bootstrap navigation menu with a drop-down where I ET rate through all categories models in the project and just just play them and as you can guess in index view I pass all categories models as context the problem is that whenever I try to access a different view let's say this privacy policy view the categories won't be displayed anymore same thing for the login view and the reason for this is very simple I did not pass categories as context to fix this issue for privacy view I just need to add categories as context for privacy view the category menu is not displayed but this is not actually a fix for the problem because first of all for login we still have the issue and I need to added the categories here as well what makes this problem somehow worse is that I may use third party Jango app for login logout for example which means I won't even have access to this login view so what we want here is to have available categories models in the context of all projects views in the views of my projects of my app where I have direct access to and in the views of third-party app like for example all all which may be responsible for sign up sign in and this is exactly where Django context processors come into the picture so what is the context processor a context processor is just a simple function which takes as input Django's request object and returns a Python dictionary the keys of return dictionary would be available in all templates of your project in other words Django context processors extend context of all views in your project please don't confuse context processors with Django middleware which is different kind of beast middleware takes as input django request object as well but the purpose of middleware is totally different Django's middleware is concerned mostly with request response cycle we will have a look at django middleware in different lesson and now that we learned what a context processors and what they are good for let's apply this knowledge context processors can live in any part of your project let's create a module called context processors in land app let's create a function here and as we learn in the theory this function will return a dictionary and actually what I will do I will copy these categories from here to here and in return dictionary I'll add key categories now let me import category model from land app now let me remove all categories keys from our views and besides adding this function we need also to configure Django project to be aware of this context processor and as you can guess we do that in settings file and Django setting in question is templates by default Django creates some default context processors and next to them we'll add our own this actually is the full path to the function which takes as input the request and returns the dictionary this one so let's have a look at our project in a browser the categories are there and home categories there good privacy categories are there as well and categories objects are available in all templates in all context of all views even if I don't specify them here instead categories are populated in this context processor I hope you enjoyed this lesson thanks for watching [Music]
Info
Channel: Django Lessons
Views: 11,538
Rating: undefined out of 5
Keywords: #django #contextprocessors #views #templates #refactotring
Id: _eWLaL2g1bo
Channel Id: undefined
Length: 5min 52sec (352 seconds)
Published: Mon Mar 23 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.