Transcript for:
Overview of Django and Web Frameworks

in this video we'll be looking into the important concepts of first module of FSD subject so I have considered the module question paper I've looked into internal paper and generated these important topics okay so let's start from the first topic that is web Frameworks and its features so this could be asked for 10 marks or if just web Frameworks is asked it could be asked for five marks so starting from the definition web Frameworks is a set of resources and tools so web Frameworks are a set of resources and tools which are used to build websit handle websites create apis and all of that so used by software developers to build and manage web applications web services as well as to develop application programming interfaces so that's what I told so they help the developers to build applications that can run on well-known technology Stacks like M mean lamp Django again being one of the technology stack okay so remember uh like three to four technology Stacks okay it'll be easy to write so then we have most Frameworks provide wide range of features and functionality that help in streamline applic development so here looking at the features first we have is the templates so application templates for presenting info within the browser then we have the programming environment for scripting the flow of information by providing an environment we can script like the flow of information uh sequences then we have the apis for accessing the backend data resources then we have code and libraries from um from code libraries we can use the um like whichever libraries are present we can use them and make it short code like instead of writing lengthy quotes you can make it make use of that right then we have quality assurance so quality assurance and debugging support is again a feature of um web framework further looking at the advantages first we have is faster and efficient application development so here this is less code and reuse then we have um when we use web framework it it becomes easy because we have lesser errors and lesser bugs then we have better performance reliability and easy maintenance and along with that we have continuous Improvement better security so web Frameworks are distinguished by their approach to application architecture many web Frameworks many web development Frameworks are based on MVC architecture we'll look into this further some might be mvvm that is model view view model or it could be push based architecture and so on so this was about web Frameworks uh looking at few more features we have here so the first one is rooting so rooting is uh you you are mapping the URLs to some specific function or class okay then we have HTTP request handling so from we we being the client we um we request some content from the server right from the website so this is again handled by the web framework then HTTP response generation when request is uh arised the request has to be handled the request has to be handled and a response should be generated to the request and this response should be again transferred to the client to us right so the that response could be HTML templates or any other data format okay so that is HTML HTML response generation then we have template engine so um web Frameworks provide a template engine through which like developers can generate Dynamic templates easily then you have database interaction next we have is the database interaction so by using modules and libraries we can interact with the database like uh here there is here we come across the concept of object relational mapping so you'll be studying this further then we have sec so in web Frameworks it provides security to the developers and addressing the common security concerns like like we have cross- site scripting prevention and session management we should make sure that the uh session management is secured and all of that then we have the session Management in session management there are many mechanisms used for managing the user sessions storing the session data onto the server using client side cookies to maintain the session rate and all then we have the user authentication user authentication means uh the web Frameworks make sure that not everyone can access the data not everyone can access the application resources only the ones who have designated roles or permissions can only access the data so this was about uh web Frameworks and its features so if they ask for 10 marks you should write all these points okay next we have the Jango FSD architecture so this may not be asked but then yeah as we are studying D Jango we should have a basic understanding of how what it is okay so Django FSD architecture full stack development architecture Jango is a high level programming high level python web framework that enables rapid development of secure scalable and maintainable web applications so here in back end we have the model layer view layer controller URL roting middleware and in front end we have the templates static file sharing integration with like reactjs angular and other softwares in database comes the postgress postgress SQL MySQL SQL light and Oracle so this was about the Django FSD architecture so Jango can come in the back end as well as in the front end so it is like connecting these two okay as well as the database also so this was about the Jango FSD architecture let's move further looking to the next topic we have is MVC design pattern MVC design pattern this is a must question so this could be asked for five or 10 months depending on the question you should write the answer okay so MVC is a software architecture plan framework or a framework that separates into three logical components one is model view and controller okay so each architectural tectural component is built to handle specific development aspect of the application so features of MVC is like clear separation we have full control over the HTML and URLs it is powerful URL mapping component and it supports test driven development so in by using MVC we can clearly segregate the business part the UI part and the input logic okay then looking at this diagram we have a person from the client side is requesting the controller to access certain data so this controller will contact the model so this model will further further fetch the data from the database and then return it back to the controller now in controller now that it has got the response it'll it'll deliver the data to the view now from views it is depicted to the user through user uh like it could be images or text data or any other HTML templates and so on so that is how MVC design pattern works so we have controller here model here and Views here controller is used to bridge the gap between views and model controller takes the request from the user and uh forwards it to the model part now model is used to interact with the backend database so back end will fetch the data from the database then and return it return the response to the controller and Views part views part is used to uh show the output to the users okay view shows the output to the users that is presentation layer you can be called as presentation layer looking at the components here components of MVC first we have this controller so controller Bridges the gap between model and view uh it processes all logic all business logic and incoming requests yes then view part view part is the presentation layer the UI logic of application it displays the data in human readable form uh like it could be web pages mobile application screen or UI component then we have the model part in model part data related to logic and business logic of application is stored in model part so model represents data and manages core functionality and of data so this was about the definition of MVC looking at the advantages the first one we have is testability so MVC supports test driven development that is it works well for like large applications where many uh developers and testers are working so without we can test the components individually without affecting the other component that is here all the classes and objects are independent of each other that is testability flexibility is again as I told so we can test separately okay and if I have to change a certain part of the application I can do it very easily it is very flexible and we have reusability that is I can use the same logic part again to develop further new application maintainability is again these things are pretty easy to maintain as like they can be extended very easily coming to the disadvantages part firstly it is difficult to read understand the model then it is not suitable for building smaller applications and uh like there is insufficiency of data access in the view part so we have complexity also overhead and learning curve again this is again a uh disadvantage increased file count there are many files which we have to create in order to access the MVC design MVC architecture then we have potential for over engineering when you can do a particular application when you can create a particular application in simpler terms why do why do you go with a complex thing then we have inefficiency of data so these are the advantages and and disadvantages of MVC design pattern if they ask this write this if they ask this for 10 marks make sure you write these components part and along with this um I hope you all know the hello world program so make sure that you write the The View part model part uh that is the URL part and the view part for this particular question okay so we'll move further next topic now we'll look into the class based views along with views even class based views is important so objectoriented way to define the views yes and it makes it easier to reuse and organize the cods along with that Jango provides various generic class based views for common tasks so this was about the introduction part let's look into the code views.py so so from Jango do views import view so here I'm importing The View class from Django views after that I'm also importing the HTTP response from HTTP okay next I have class My Views uh view so here I'm creating a new class named my view that inherits from the views class after that def get self comma request so this line defines a method called get within the my view class next I'm returning the HTTP response hello world looking at the URL part here I have from Jango URL importing the path and along with the path I'm also importing the uh home like from do views import home so this Line Imports the home view from the views model in the current directory only next I have URLs is equal to par this is the empty string which I'm declaring here after that the second argument is home. as view so it refers to the home view which we have imported before okay after that we have name is equal to uh home that is we are specifying the URL name okay and this is how we are importing the home view so this was about class based views let's look in let's let's look further now let's look into the evolution of D Jango so it was created in 2003 by these two people adren HTI and Simon Willison while working for a newspaper company in Kansas so it was built initially to maintain the website's content okay so it was BD BSD licensed in July 2005 then it gained popularity due to batteries included approach so it provides a set of tools to build web application web applications with uh dry Dr principles then uh the growth in the Jango Community happened through through the invention of plugins and tutorials so many Jango versions were released from 1.0 to 5.0 so it provides a long-term support that is it provides stability and maintenance updates it is uh it has stable code based for projects new features are provided in this on a regular basis it supports for complex database queries and improved handling of static files and media so along with this uh the as the Jango is evolving it has active community and ecosystems so there are multiple libraries coming up there are third party libraries tools and resources along with this Django is again simple flexible scalable and popular choice so if they ask about evolution of D Jango no so make sure you write all these points and if they ask about specific features be it the drive principles or battery included approach make sure you expand it and write properly and uh next moving on we have the views part so understanding the definition of views so python function then takes web request and returns a web response yeah so views are the response view response can be generated can be web page contents that is HTML or redirect to another page or it could be error also or it could be XML document or image or anything so the view itself contains whatever arbitrary logic is necessary to return that uh response looking at one more definition so views is a python function used to perform business logic and return the response to the user we have seen this in MVC definition right so this was about the views part if they ask Define views you can write the these part these points okay now we look into the project structure so firstly we create a project uh in Project directory we have these many uh different different files so the first one is manage.py so it is a command line utility help to interact with the Django project then you have settings.py it has all the configuration details about the project so if I have to update any database and all I should do it in the settings.py then we have urls.py that is used to map the URL paths to the view of application then we have the wsgi so this is web server Gateway interface it it provides an entry point for wsgi compatible servers then we have aspy that is asynchronous server Gateway interface used for asynchronous applications then we have um under um under this project directly when we create an application apps we'll be looking into models views URL template and migration we'll discuss this further okay so now let's look into project creation um execution and commands so firstly we have installation of python and Django or and any code editor so make sure that you have installed python P install python then pep install Django check out the versions pip version python version python M Django version to get the updated versions then installation of virtual environment tool to create isolated python environment so it is used to avoid conflict between the projects that is p install virtual EnV next we create a new directory for project Jango project and navigate to it uh uh through command prompt then we create virtual environment by running so we are creating a py lab that is a directory along with that uh we are changing the directory changing the path to CD PAB after that we are creating the virtual environment EnV EnV is the name of virtual environment okay then we activate the virtual environment once we have created the virtual environment we activate it using EnV being the name of the environment scripts SL activate then start a new project new project the name of the project is my project so Jango admin start project new project my project so in any program if they write if they ask us to create a application make sure that you uh start from this particular command that is dango admin start project name of that project change the directory directory to that project and then uh you can uh like run python my server dot run the devop development server that is python manage.py run server so when we do this we'll see output indicating the development server is running so we can open the browser browser and navigate through this uh URL so we can see that Django Project's default landing pages uh reached next we have uh after this we have to create a project create an application right project is created my project now I'll create an application so Pauls okay so python manage.py start Pauls is the application is the application name now I visit the code editor uh vs code I'm using so I open the folder select the PAB folder in which we have created so inside that I have Pauls being the application name we have following laid out that is inet py then we have admin.py appspy migrations inet py models.py test.py and view.py so now I select View and write the python code so to select view I click on this view part then we have the page so here I can write whatever the python code is and save it after that I create the URL configuration in Paul's directory create a file called urls.py so under this only app / urls.py and I write the URL to perform mapping URLs to the Views part then after saving these two again in the settings part I need to update right so after updating in the settings part settings URL also I'll run python manage.py run server so this is how uh like the basic creation of a project creation of a application and execution of project and application works so if they ask this in exam you should write all these steps and explain it accordingly to understand uh we look into a hello world program so in this module if they ask us to write example for MVC architecture or explain views part or anything no just make sure you write this okay so in Pauls the same being the application name and views.py I write from Django HTTP import HTTP response I am importing HTTP response so we are defining a function that is index and request is the input so along with that we have uh return HTTP response this respon the returning response would be this particular text that is hello world so to call the view we need to map uh it to a URL and for that we need URL configuration so we create a u uh in our Pauls directory we create a URL okay so paul/ URL here we write from Django URL import path we are importing the path along with that Jango from do import views now we'll update the will specify the URL part URL patterns is equal to path and here we have um open in the bracket we write views. index from the views we are accessing it right and name is equal to index here we are again specify the name of that your that particular view part okay next we have that that particular function okay next step is to point the root URL at ps. URL module so in my in my side/ urls.py add an import from Jango Django url. include and insert an include function in url pattern list so we have it as this so in my site url.py I get it as from O.P import admin so I'm importing the admin part along with that I should include the path also no so I I'll have import path comma include I'll I'll write URL is equal to path Pauls include paul. URL along with that the admin part would remain as it is admin SL admin. site. URLs so here include function allows referencing other URL configurations so when Jango encounters include part it chops off whatever part of URL matched up to that particular point and sends the remaining string to included URL configuration for further processing so the main idea is to make it easy for Plug and Play URLs then we execute the command by seeing python manage.py run server we have the URL we have the URL generated here when we click that we get the output as hello world so this was about U hello world program okay let's further look into the next topic we have is mapping URLs to views so in Django URL configuration acts as a table of contents so URL configuration access table of contents for our Django website so it Maps URLs to view functions instructing D Jango on which code to execute when a specific URL is accessed so when we generate a project Jango hyphen admin.py start project the script automatically generates a URL configuration file named url.py this will be in settings part we know that so this file will have the necessary configuration to map URLs to view so next we import necessary modules from Jango URL configuration infrastructure that is typically we import all objects from Jango configs url. defaults so this import will include patterns function which is used to define URL pattern so we take a base URL that is empty string as its first argument this is the empty string so URL is equal to patterns empty string so the result is stored in a variable called URL pattern here okay so to map specific URL to a view function we make use of regular expression uh string or add the corresponding view function in the tle so to map hello URL to hello view function we first import it that is from My site. Views import hello that's what we did in the previous hello program also right we imported the path right then URL is equal to pattern this is the empty string the base URL which you are taking then uh hello dollar comma hello this is the name we are specifying here so this tells Jango that any request to hello URL should be handled by hello view function so here we make use of the regular Expressions so this symbol represents that the asset assets start of start of string so here we have the symbol that asserts start of the string hello slash we have is SL we have is to match the literal string and dollar is to end the particular string so this was about mapping URLs to views so again if they ask an example to write no make sure you write the hello world program next we have types of errors in Jango so Jango can encounter various errors during development and deployment the first one is template errors so it occurs when Jango encounters issues processing the template files so this could be due to syntax error in the HTML template or there could be undefined template variables or problems with the template tags itself so this was about template errors then we have the view errors so the problem with python code in view function can arise to view errors so this could include the syntax errors or exceptions not handled properly or it could be issues with the logic we have implemented then we have the URL resolution errors so Jango cannot find matching URL pattern for requested URL so that's when this type of error can occur so might happen because of mistake while typing the URL patterns again again human error so incorrect regular expression or conflict between URLs are mapped so because of this URL resolution of Errors occur then you have the database errors database errors means when the Jango is interacting with database there might be errors because of wrong credentials or invalid SQL syntax or problem with the database schema itself then you have the server errors that is misconfigure the entire misconfiguration or any memory issues or any um permission issues and all so in order to debug the Django errors we can follow the tips that is consult error messages as it is given as it gives detailed info and understand the nature of Errors so if you have seen the error page here the type of error would be specified and in the bottom uh a particular uh explanation would be given what sort of error it is and how to uh trace the clues okay that was about first point second we have is to utilize Jango debug mode so it provides extra debugging information and AIDS to identify errors during development itself then to check the Jango documentation again we can use debuggers like py Cham and pdv so this was about errors in Jango if they ask right make sure you write these Five Points along with that you write this how to debug the errors okay next we have is this part how J processes are request so again these these are all Theory questions no they have they could be asked for five marks okay so Theory and if there are any diagrams make sure you draw the diagrams also so when we run the command python manage.py run server it Imports a file called settings py from the same directory we know this so this file contains all sorts of optional configurations for this particular Jango instance but most important setting is root URL config conf so this tells Django which which python module should be started as URL config for this particular website so when we when request comes for example time to Jango it loads the URL config pointed uh to by the root URL config setting and then it checks each URL pattern in the URL config in order until it finds that the one particular U like URL which you are searching like time if time is found after finding the Matched URL it calls the view function associated with it and then passes the http request object as its first parameter to the function so the view is uh function is responsible for returning the HTTP response so from browser HTTP model python Handler and here HTTP request we have a request middleware from this if it is found we go further that is URL config from there if view is found we come to the view part View middleware and then come to the view part now from The View whatever HTML code we have specified would be um like it would be we look into that and after that a response would be generated that is uh exception middleware so if you is not found you exception Handler then we directly provide the exception after this we provide a response through the middleware and back to the python Handler so that is how here we generate the HTTP response so if asked how does the Jango processor request make sure you draw this diagram and explain it in it is pretty simple topic right you can explain it in your in your own words next we have URL config and loose coupling this is again a pretty important topic it could be asked for 10 marks also so starting uh we have in Django URL config and loose coupling go hand in hand as a way to create modules modular and maintainable web applications so understanding the definition of loose coupling it is a it is a software design principle that emphasizes on minimizing the interdependence between different parts of code so uh there are cases where a particular code is inter dependent on the other code right but Loose coupling its aim is to minimize the interdependence between different parts of code this means that change made to one section of the code should not be affected on the other section of the code or uh in the other words if I make change in this particular code there should be minimal or no changes on this particular code so that's what is said here this means change made in one section should be minimal or no impact on the other section so Jango URL config are python files that Define how URLs map to the Jango functions we know URL configuration so this separates uh this separation between URL definition and view implementation promotes loose coupling in following ways suppose you want to create you want to change URL of a particular view like from articles to blog SL articles you can simply modify the URL configuration without touching the view part right so this is we we all can do this pretty easily like if we look at the hello uh hello program we can easily change its name def def in the def part we can can easily change right uh suppose you want to modify a functionality within a view function we can update the code in view function without affecting the URL pattern so if I have to modify the view function I can again change it without affecting the URL pattern suppose hello program is there right hello world is there I can write hello world V so by changing the view part also there is no impact on the URL section right so this allows us to evolve The View logic without breaking the URL mapping again understood so a simple function can be mapped to multiple URLs that is we are promoting the code reuse part so looking at the benefits of loose coupling so so it is like independent changes to URL and Views can be made so makes code easier to maintain de debug and modify yeah then we can easily add new functionalities without affecting the existing URLs flexibility so both URLs and Views can be tested in isolation simplifying the isolation process and we have scalability that is I can add n number of uh new functions into the view part or I can modify the URL structure then we have the reusability that is a single function can be M mapped to multiple URLs so again promoting the reusability code reuse part so URL configurations and loose coupling is a pretty easy topic uh if if if it has asked for 10 marks no make sure you write this much all this content along with this you explain the hello world program doing necessary changes for this particular part do one change in the URL part and for this particular part change in the view part okay looking at the Wild Card patterns again important question 10 marks so it is a powerful tool for handling a wide range of URLs with a single definition so we know Wild Card patterns so these are the URL patterns URL patterns are generated using the regular Expressions so um this wild card patterns it is like a powerful tool used to handle a wide range of URLs with single definition that is it is important to use them properly and understand its potential drawbacks wild wild card patterns are created using the re paath function re paath function from Django do URLs and typically includes do star notation so this notation matches any number of characters in the URL path so the advantages for this is conciseness so they simply they simplify the URL definition for handling many similar URLs so if I draw if I put dot star no this means that any number of characters could be matched for the URL path then you have the exibility so a single pattern can accommodate various URL structures that's what we have looked in the definition part looking at the drawbacks so it is less readable because they will have complex regular Expressions so if they are not created properly know then it might match unintended URLs if I have to match views.py it it might match to suppose if I have to match a particular URL uh if it is not defined properly know I might end up matching some other uh page okay further looking at the alternative options so multiple specific patterns uh for better maintainability either we can use multiple specific patterns for uh each of the URL or name captured groups can be used within the Wild Card pattern to capture specific parts of URL while avoiding any overboard matches so alternate options of wild card URLs could be multiple specific multiple specific patterns or it could be name capture groups or being used so when to use the URL pattern so we use them sparing and only when a single wild guard pattern offers a clean clear benefit in terms of conciseness and readability compared to the multiple specific multiple specific patterns so if we need to capture specific parts of URL employ named captured groups we can make use of wild guard patterns so wild guard patterns again being a theory topic you can write it easily next we have is what are the best practices for uh Wild Card patterns so Clarity over convenience so the code has to be clear and maintainable code uh then we have documentation patterns so we have to document the Wild Card pattern to understand its purpose and potential limitations then what are the Alternatives that is to evaluate if have to check if specific patterns or name captured groups can achieve more maintainable code than the Wild Card pattern looking at the regular Expressions if you have dot it is any character /d means single digit or if I have to include three digits I can I can write D and include three then upper case lower case I hope you know this and for selecting the case sensitive characters I can go from A to Z A to Z again we have studied all this in Python lab right then we have plus that is one or more previous expression and we have these symbols depicting one or more characters until the forward slash and if you have question mark it is zero or one previous expression if you have star it is zero or more of previous Expressions then this H this depicts the range between 1 to three next we have uh the program that is Jango application that displays current date and time in the server so we have python manage.py start app we are creating an application my app so assuming that we have we are making use of U let's say my project okay under my project directory we are making use of we are creating a application my app so for in views part I have to write the logic right so from Django HTTP import the http response then I Define from date time I am importing date time date time function okay so Define current date time current date time being the function name and request what am I requesting now is now is a variable I am obtaining the current date and time okay date time do now now specifies that particular instance this um pre-built library from date time Library I importing this function and I'm using it here so now now in now variable the current date and time would be stored now again in HTML HTML body I am giving the title as current date and time and here I'm specifying the now variable and along with that um/ body/ HTML I'm closing it and I'm returning response as HTML as in HTML this entire HTML part was stored right so I'm returning HTML so this was about the views part looking at the URLs part from Django URL import the path and I'm importing the views also and after that URL path is equal to I'm updating the URL so that it has to be current date time this function name current date time from views use. current date time I'm accessing this from use. current date time name should be current date time so again specifying uh like uh the URL pattern name okay next we have now that I'm done with the URL part next I'll move into settings / url.py so here Django Contra import admin I'm importing admin along with that I'm importing path and include function so we have seen in the previous hello world program right so the same logic applies here URL pattern is equal to path admin path admin would be there as it is along with that I'm also adding my path of the project which I have created that is my app and include my app. URLs my app URLs are also included here after that python manage.py so when I run this program I'll get the current date and time as the output next this was for to display current date and time next we have date and time 4 hours ahead and before that is offset value so here offset value if it is 10 entire program is same just uh here offset r value only we should change so my app again I've created an application with the name my app in views part I'll be writing Django HTTP import HTTP response Django date time import date time and time Delta so I'm importing date time along with that I'm also importing time Delta so Define date time with offset the function name and request so I'm providing a request now is equal to date time do now so I get the present date time here next offset R is equal to 4 offset R is a variable this has to be in the next line okay so four FHA FHA is equal to now plus time Delta and here I'll be specifying the offset value that is heart is equal to offset hours along similarly 4 hours before for 4 hours before f fhb is equal to now minus time Delta RS is equal to offset minus offset RS specifying this value so further once I have got the FHA and FHP value along with no value I'll provide the HTML um output that is HTML body defining it as current date and I'm providing the output now and next I have 4 hours ahead I'll be providing FHA 4 hours before fhb and closing this HTML part I'll be returning HTML HTTP response HTML so I'm done with my view part in my apps uh in my app / url.py again import the path views uh now path date time with offset defining the whatever the function I'm calling it here mapping it here right views. date time with offset again calling it here and here I'll be specifying the name and closing it and in settings part it's again the same here python manage.py run server so this was about uh the first module here I've tried covering all the important topics if you have any doubts do let me know in the comments and stay tuned I'll be uploading second module third module soon so yeah thank you