

{"id":74081,"date":"2020-01-04T10:54:18","date_gmt":"2020-01-04T05:24:18","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=74081"},"modified":"2021-06-21T12:19:10","modified_gmt":"2021-06-21T06:49:10","slug":"django-interview-questions-and-answers","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/","title":{"rendered":"Python Django Interview Questions and Answers &#8211; Get hired as Django developer"},"content":{"rendered":"<p><strong>Django Interview Questions and Answers<\/strong><\/p>\n<p>After practicing the basic interview questions, it&#8217;s time to level up with Django interview questions for experienced professionals. These questions will help you with the technical interviews for a backend-developer role. I assure these Django interview questions and answers are sure to help you get your job as a Django developer.<\/p>\n<p>We have a complete series for your Django interview preparation:<\/p>\n<ul>\n<li><a href=\"https:\/\/data-flair.training\/blogs\/django-interview-questions\/\"><strong>Django Interview Questions and Answers for Freshers<\/strong><\/a><\/li>\n<li><strong>Django Interview Questions and Answers for Experienced<\/strong><\/li>\n<\/ul>\n<p>Let\u2019s get started.<\/p>\n<h2>Python Django Interview Questions and Answers<\/h2>\n<p>Here is the list of advanced Django interview questions and answers for your practice:<\/p>\n<p><strong>Q.1 Explain context variable lookups in Django.<\/strong><\/p>\n<p><strong>Ans.<\/strong> Context variables are variables passed in templates. The DTL (Django Templating Language) replaces these variables. A context dictionary is used to perform the replacement. We pass the context dictionary alongside the render() alongside template information.<\/p>\n<p>DTL has its own way of filling these variables and it\u2019s in order. The template system can handle complex <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/python-data-structures-tutorial\/\">Python data structures<\/a><\/strong><\/em> as variables. The context variable lookups come in the role when the data is a dictionary, class object, etc.<\/p>\n<p>The context lookup will fill the value in this order.<\/p>\n<ul>\n<li><strong>Dictionary Values:<\/strong>\u00a0The template system will look for dictionaries matching the variable name. It matches the key name with the name after the dot(.) operator.<\/li>\n<li><strong>Class Object Attribute values:<\/strong>\u00a0If it didn\u2019t find any dictionaries it will look for a class object. First lookups are done for attributes.<\/li>\n<li><strong>Class Object Methods:<\/strong> It looks for a particular attribute of that name. If the attribute is not found then it looks for methods.<\/li>\n<li><strong>List-Index lookup:<\/strong> At last, it will look for any lists with corresponding names. If it didn\u2019t find any lists then it considers variable as an Invalid Variable.<\/li>\n<\/ul>\n<p><strong>Q.2 How does DTL handle invalid variables?<\/strong><\/p>\n<p><strong>Ans.<\/strong> There are various instances where a variable might not exist in context variables. In case DTL cannot find the variable in context, it considers that variable as an invalid variable.<\/p>\n<p>The DTL handles the value for invalid variables. The DTL System will insert the value from a setting in these cases. The setting is an attribute of the engine named as string_if_invalid. By default, it\u2019s an empty string. That empty string fills in the value in HTML render.<\/p>\n<p><strong>Q.3 Can we call methods in templates? Elucidate with example.<\/strong><\/p>\n<p><strong>Ans.<\/strong> Yes, method calls are possible in templates itself. it\u2019s the same thing as accessing an attribute from an object or dictionary.<\/p>\n<p>For example, we have a Python class:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/Python-class.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74166\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/Python-class.png\" alt=\"python class - django interview questions and answers\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/Python-class.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/Python-class-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/Python-class-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/Python-class-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/Python-class-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/Python-class-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>We make a template<strong> t at line 9<\/strong>.<\/p>\n<p>In this template<strong> t<\/strong>, we will get the output of the <strong>online<\/strong> method in <strong>DataFlair<\/strong> Class. The corresponding output can be seen in the image. It is after line 11.<\/p>\n<p>As we can see it prints the output returned by the string. We simply created a context dictionary and passed it to the template. The context dictionary has a key DataFlair which contained a DataFlair class object. To note, we execute the method when passing the object in the context variable. It is not recommended to pass methods like that. The method execution takes place before rendering.<\/p>\n<p>Thus, we can call methods in the template.<\/p>\n<p><strong>Q.4 How can we stop the execution of the method while rendering?<\/strong><\/p>\n<p><strong>Ans.<\/strong> It is true that the template system shall not be given access to some methods. Methods that can change data in the database are one of them. To prevent the execution of the method, we can keep a lock on the method definition itself.<\/p>\n<p>DTL also checks for some variables in methods automatically. The variable we want to put in our method is <strong>alters_data = True<\/strong>. If we keep that variable in our method, the template system won\u2019t render it. Instead, it will treat the context variable as an invalid variable.<\/p>\n<p><strong>For example &#8211;<\/strong> Suppose we have a method that contains this variable.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/method-containing-variable.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74167\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/method-containing-variable.png\" alt=\"method containing variable - Django inteview questions and answers\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/method-containing-variable.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/method-containing-variable-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/method-containing-variable-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/method-containing-variable-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/method-containing-variable-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/method-containing-variable-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Taking the template from the previous question, we just pass this method. Now, when we render this template, it will print output as shown.<\/p>\n<p>Thus, the method was not executed and none object rendered.<\/p>\n<p><strong>Q.5 What is forloop variable? Explain and implement.<\/strong><\/p>\n<p><strong>Ans.<\/strong> The <strong>{% for %}<\/strong> in the template system comes with a pre-defined template variable. The forloop variable has various attributes that are frequently used. The forloop variable attributes are:<\/p>\n<ul>\n<li><strong>forloop.counter:<\/strong> The forloop counter will return the integer. The number of times the loop has run is displayed in it. The forloop counter starts counting from 1.<\/li>\n<li><strong>forloop.counter0:<\/strong>\u00a0It is the same variable as the forloop counter but the counting starts from 0.<\/li>\n<li><strong>forloop.revcounter:<\/strong> The forloop reverse counter returns an integer. The number of times the loop will run or the number of items remaining. Its numbering ends on 1.<\/li>\n<li><strong>forloop.revcounter0:<\/strong> It is the same variable as forloop reverse counter. The numbering ends on 0 rather than 1.<\/li>\n<li><strong>forloop.first:<\/strong>\u00a0This is a special variable that returns True or False. This variable is true when the loop runs for the first time and false for all other times.<\/li>\n<li><strong>forloop.last:<\/strong>\u00a0This variable is similar to forloop.first. the only difference is that it marks the end of the list. It returns true when the list has ended and false otherwise.<\/li>\n<\/ul>\n<p>In the below example, we have implemented For loop in the template.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/for-loop-implementation-in-template.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74168\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/for-loop-implementation-in-template.png\" alt=\"for loop implementation in template - python Django inteview questions and answers\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/for-loop-implementation-in-template.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/for-loop-implementation-in-template-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/for-loop-implementation-in-template-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/for-loop-implementation-in-template-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/for-loop-implementation-in-template-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/for-loop-implementation-in-template-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>The output of the template is shown.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/template-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74169\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/template-output.png\" alt=\"template output - Django inteview questions and answers\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/template-output.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/template-output-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/template-output-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/template-output-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/template-output-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/template-output-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>As you can see, the variables and their values. That\u2019s the functioning of forloop variable and its attributes.<\/p>\n<p><strong>Q.6 What is reverse URL resolution in Django?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Reverse URL resolution is the method of obtaining urls in their final forms. The URLs can be embedded in generated webpages. Like in the navigation bar, there are various scenarios. Django provides various methods to achieve this which otherwise could have been hardcoded.<\/p>\n<p>Hardcoding these URLs is a time-consuming and error-prone task. Therefore, these ways are used instead of getting Url&#8217;s final form. The reverse resolution of URL is commonly achieved:<\/p>\n<ul>\n<li>In templates, by using <strong>url template<\/strong> tag.<\/li>\n<li>In Python code\/view function, by using the<strong> reverse().<\/strong><\/li>\n<li>In some high-level model class, by using<strong> get_absolute_url()<\/strong> method.<\/li>\n<\/ul>\n<p><strong>Q.7 Explain user object in the Django admin system with its 3 flags?<\/strong><\/p>\n<p><strong>Ans.<\/strong> User objects are our standard objects which have attributes like username, password, e-mail and name fields. The user objects have a set of fields that define the permissions for that user.<\/p>\n<p>All users are not allowed to do everything and that\u2019s where Django\u2019s permission system comes in. Its an integrated part of <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/django-admin-interface\/\">Django admin<\/a><\/strong><\/em>.<\/p>\n<p>There are 3 flags which determine the user activity, they are:<\/p>\n<ul>\n<li><strong>Active Flag:<\/strong> The active flag sets whether the user is active or not. this flag shall be on if the user wants to login otherwise system won\u2019t log in the user. Even if the credentials entered by the user are correct this flag still needs to be active.<\/li>\n<li><strong>Staff Flag:<\/strong> This flag differentiates between staff members and public users. It checks whether the user can access the admin-site. If he\/she is not a staff, the response by the server is an error. Often a normal redirect is used in these case scenarios.<\/li>\n<li><strong>Superuser Flag:<\/strong> The superuser is the ultimate user of the admin site. It can Create, Update, Delete any object. If this flag is on then all regular permissions are available for this user. It can perform any sort of operation that exists in Django-Admin.<\/li>\n<\/ul>\n<p><strong>Q.8 What are object permissions in admin?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Each object which can be edited in the Django admin has three permissions. <strong>Create Permission<\/strong> allows the user to create the objects one time. An <strong>Edit Permission<\/strong> allows the user to update the created objects. A <strong>Delete Permission<\/strong> allows the user to delete objects.<\/p>\n<p>These permissions have an impact on the database too.<\/p>\n<p><strong>Q.9 What are custom validation rules in form data?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Custom validation rules are the customized rules used for form validation. Suppose we have a feedback form. There are fields like <strong>messages, email, and subject<\/strong>. If we get message data of 1 or 2 words that are of no use to us. To check the issue, we use custom validation rules.<\/p>\n<p>We simply define a method in our forms.py by the name <strong>clean_message()<\/strong>. This is the Django way to do it. The method\u2019s name for custom validation should start with a <strong>clean_fieldname()<\/strong>. Django form system automatically looks for this type of method.<\/p>\n<p>Thus, these are called custom validation rules in Django.<\/p>\n<p>It is always important to return the cleaned data of the field in a custom validation method. If not done, the method will return none instead resulting in loss of data.<\/p>\n<p><strong>Q.10 What is the functionality of django.contrib.auth app? What kind of system exists in the auth app?<\/strong><\/p>\n<p><strong>Ans.<\/strong> The auth app is a built-in Django application. It provides the developer with an authorization system. The auth app handles both authentication and authorization of users.<\/p>\n<p>The authentication system consists of:<\/p>\n<ol>\n<li>Users<\/li>\n<li>Permissions<\/li>\n<li>Groups<\/li>\n<li>Password Hashing System<\/li>\n<li>View functions and an interface<\/li>\n<li>Customizable and plugins for the backend system<\/li>\n<\/ol>\n<p><strong>Q.11 What is the difference between authentication and authorization?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Authentication and authorization are two different terms. The authentication means the verification of the entity(user) in Django\u2019s context. Authentication will verify that the user is what they claim to be.<\/p>\n<p>Authorization is the step after authentication. It sets the actions that can be performed by the authenticated user. Authorization is a grouping of users and allowing limited actions.<\/p>\n<p>Both of these functionalities are achieved by <strong>django.contrib.auth application<\/strong>. It is a built-in application in Django.<\/p>\n<p><strong>Q.12 What\u2019s the difference between these two commands? What result will their execution generate?<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/commands-for-django-project.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74170\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/commands-for-django-project.png\" alt=\"commands for django project - Django inteview questions and answers \" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/commands-for-django-project.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/commands-for-django-project-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/commands-for-django-project-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/commands-for-django-project-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/commands-for-django-project-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/commands-for-django-project-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p><strong>Ans.<\/strong> Both commands are used to create or initiate a Django project. The difference comes by adding the period (.). The period makes it so the project initiation differs.<\/p>\n<p>The command without the period(.) will initiate the project in a new directory. That means all the files will be in an additional sub-directory. The later will initiate the project and keep all the files in the same directory.<\/p>\n<p><strong>Q.13 Explain the usage of django.contrib.auth.get_user_model()?<\/strong><\/p>\n<p><strong>Ans.<\/strong> The user model is the base model. Developers need it customized for their application. It should also contain all the security features that Django offers. Usually, they only want the inherited class and add or remove the field option there.<\/p>\n<p>Now, get_user_model() will let us reference to user model. This method allows developers to use the custom user model. It is changed in setting AUTH_USER_MODEL in <strong>settings.py<\/strong>. The <strong>get_user_model()<\/strong> allows the developer to implement the changes. It can reference the custom user model where needed while avoiding global implementation. Since Django\u2019s user model is tightly bundled with admin and auth. It is beneficial to customize Django\u2019s user model.<\/p>\n<p>The get_user_model() comes in here and resolved that. The get_user_model() will return an active user or a custom user. In case none of the two are present, it will return the user object.<\/p>\n<p><strong>Q.14 Why do we need performance benchmarking? What is a tool that can be used to do the same?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Performance improvement is an important factor. It can make the difference in your consumer base. The performance measurement tools is thus an important part. If you want to improve the performance of your web platform, it needs monitoring and analysis. Performance-benchmarking tools help us analyze website performance.<\/p>\n<p>For Django, there is a great tool available: <strong>django-debug-toolbar<\/strong>. It will monitor and provide you with various panels.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-debug-toolbar.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74171\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-debug-toolbar.png\" alt=\"django debug toolbar - Django inteview questions and answers\" width=\"991\" height=\"647\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-debug-toolbar.png 991w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-debug-toolbar-150x98.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-debug-toolbar-300x196.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-debug-toolbar-768x501.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-debug-toolbar-520x339.png 520w\" sizes=\"auto, (max-width: 991px) 100vw, 991px\" \/><\/a><\/p>\n<p>These panels show information for very niche things. Like the SQL panel here will show how much time it took to execute the SQL. The tool is significant and can give you leads to where you can improve the performance. This is currently the no.1 benchmarking tool which integrates easily with Django.<\/p>\n<p>This is an internal tool. One, that needs to be included in code. There are other ways of performance benchmarking. There are third-party services like Yahoo\u2019s Yslow and Google PageSpeed. They will analyze the web platform as an HTTP Client. They give the performance as experienced by the user. These are free and many more services are available both free and paid.<\/p>\n<p><strong>Q.15 What is the use of cached_property() decorator?<\/strong><\/p>\n<p><strong>Ans.<\/strong> The<strong> cached_property()<\/strong> decorator is a built-in decorator. This decorator comes under the <strong>django.utils.functional<\/strong> module. The decorator\u2019s property is to take the method as an argument. The method\u2019s result is cached by the decorator for that instance.<\/p>\n<p>This is useful when a method is computationally expensive. Also, its generated value is valid for a while or for the session.<\/p>\n<p>We can use this decorator just before we define the computationally expensive method. That method\u2019s answer is cached for the instance.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/cached_property.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74172\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/cached_property.png\" alt=\"cached_property() - Django inteview questions and answers\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/cached_property.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/cached_property-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/cached_property-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/cached_property-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/cached_property-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/cached_property-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>Replace the pass with functional code. Also, import the method from <strong>django.utils.functional<\/strong>. These modules are provided by Django to handle tedious tasks.<\/p>\n<p>These also support backward compatibility. The <a href=\"https:\/\/pypi.org\/project\/django-model-utils\/\">utils<\/a> module of Django is a very powerful set of tools.<\/p>\n<p><em>Any difficulty in the Django interview questions and answers till now? Mention in the comment section.<\/em><\/p>\n<p><strong>Q.16 What is the concept of laziness? How Django implements laziness?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Laziness is a complement of caching. We use both the processes to improve the efficiency of the program.<\/p>\n<p>We <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/django-caching\/\">implement caching<\/a><\/strong><\/em> by storing the result of a computationally expensive result. But, we implement laziness by not computing it until it\u2019s actually required. The concept of laziness is doing things when it&#8217;s necessary. This approach has its own benefits. The more expensive the computation, the more we avoid it. It does work when it becomes a necessity.<\/p>\n<p>Django is quite lazy. The most used example of laziness is querysets. We have used querysets quite often. The queryset objects are passed around even before they have the value. This approach is where we pass objects. Defining methods before the definition of its data, we call it laziness. It makes very easy for us to interact with databases.<\/p>\n<p>Python support and encourages laziness. That makes it up for its performance issues many times.<\/p>\n<p>It\u2019s a concept made by lazy people implemented in computers.<\/p>\n<p><strong>Q.17 Django provides various optimizations for static files. Explain one of those solutions.<\/strong><\/p>\n<p><strong>Ans.<\/strong> Static files are one of the main contents consuming the bandwidth of the client. There are times when a low network speed results in a poor experience for the consumer. There are various solutions to deal with this in Django.<\/p>\n<p>Django developers came up with a solution using the browser\u2019s caching behavior. The <strong>ManifestStaticFilesStorage<\/strong> class is the implementation of the solution.<\/p>\n<p>The ManifestStaticFilesStorage class uses the browser caching ability. Using this class will improve site performance at low network speeds. The class when serving the static file appends a content-dependent tag with the file. The tag lets the browser store the file for a longer time. Django changes tag only when there is some change in the file. Otherwise, the browser uses the stored file.<\/p>\n<p>The ManifestStaticFilesStorage serves the files with the names added with the MD5 Hash. Django generates the Hash from the content of the files. For example:<\/p>\n<p>The filename is<strong> DataFlairStyle.css<\/strong>. The file stored by ManifestStaticFilesStorage Class is <strong>DataFlairStyle.55e7cbb9ba48.css<\/strong>.<\/p>\n<p>Django serves the custom file to the browsers. Then if the file content changes do the MD5 hash. Thus, the browser will know which file to download. Browsers mostly cache the static files. It&#8217;s easier to just change the name if anything is changed in the backend. The browser will download it again if the file name changes.<\/p>\n<p>That\u2019s how we can improve the static files serving.<\/p>\n<p><em><strong>Take a break from Django interview questions and answers and <a href=\"https:\/\/data-flair.training\/blogs\/django-static-files-handling\/\">Learn Django Static Files Handling<\/a><\/strong><\/em><\/p>\n<p><strong>Q.18 Explain the management commands of django.contrib.staticfiles app.<\/strong><\/p>\n<p><strong>Ans.<\/strong> The staticfiles app comes with three useful management commands. The commands are:<\/p>\n<ol>\n<li>collectstatic<\/li>\n<li>findstatic<\/li>\n<li>runserver \u2013nostatic<\/li>\n<\/ol>\n<p>We can execute the collectstatic command like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py collectstatic<\/pre>\n<p>As its name suggests, this command will collect all the static files from the directories. Django scans the directories in the setting STATICFILES_DIRS. All the static files found are then collected in directory STATIC_ROOT.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py findstatic staticfile [staticfile]<\/pre>\n<p>This command will return the path of static files found. The arguments contain the file name. We can append the filename within the directory to make the search more efficient.<\/p>\n<p>Django searches files in directories set in STATICFILES_DIRS setting.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py runserver<\/pre>\n<p>This command can be confusing. The runserver is not a staticfiles management command by default. If the django.contrib.staticfiles is installed in Django, the runserver command is overridden.<\/p>\n<p>The runserver command gets various options like:<\/p>\n<ol>\n<li><strong>&#8211;nostatic :<\/strong> Serve requests without loading static files.<\/li>\n<li><strong>&#8211;insecure :<\/strong> This command will force the app to serve static files even if the debug command is off.<\/li>\n<\/ol>\n<p>These are the 3 management commands which we get with staticfiles app in django.<\/p>\n<p><em><strong>Projects are very essential for gaining practical experience. So, work on the <a href=\"https:\/\/data-flair.training\/blogs\/django-project-news-aggregator-app\/\">News\u00a0Aggregator Django Project<\/a><\/strong><\/em><\/p>\n<p><strong>Q.19 What is Pagination?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Pagination is a concept where we separate or divide data into different pages. It&#8217;s divided into multiple pages. The pages are provided as per user-request.<\/p>\n<p>In the context of web applications: Suppose you search for anything on Google. Though Google claims they have found thousands of results in the resulting page, they give you only 10-20 results. Then you press the next page and you get more results. That is pagination.<\/p>\n<p>Showing user only limited data is good for both of them. The user can sort the results easily and smoothly reach the information he\/she wants. The bandwidth cost is not much for some results. Since the transfer-data is small, it gives results on low network speeds.<\/p>\n<p>Pagination is also good for the server. The server can simply store the data in queryset or database and wait for the client to request more. This reduces the template overhead which would have been to generate a response. Generating a response with 1000 data is much more expensive than 10-20 data.<\/p>\n<p>Pagination can drastically improve performance if done the right way.<\/p>\n<p><strong>Q.20 How do we implement pagination in Django?<\/strong><\/p>\n<p><strong>Ans.<\/strong> We implement pagination in Django with the help of <strong>django.core.paginator<\/strong> classes. These classes help us manage and use paginated data.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django.core_.paginator.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74173\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django.core_.paginator.png\" alt=\"django.core.paginator\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django.core_.paginator.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django.core_.paginator-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django.core_.paginator-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django.core_.paginator-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django.core_.paginator-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django.core_.paginator-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>From the code above, we have a list named <strong>datalist<\/strong>. That list is then divided into 2 pages. We can think of pages as numbered elements or parts of the object. The division occurred when we pass the list in the class constructor. The second parameter specifies the number of objects in one page. The pagination can be directly implemented in views.<\/p>\n<p>The first argument can be a list, tuple or a Queryset.<\/p>\n<p>In lines 7 and 8, we used some built-in attributes of Paginator class. The <strong>count<\/strong> will give the number of objects. It is the total number of objects. The <strong>num_pages<\/strong> will provide the number of pages made.<\/p>\n<p>In this image, we are accessing the page data. We can access the page data with <strong>pages.page_range<\/strong>. This function works the way as a range in for loop. We can use object_list to access the contents on the page. It gives the list of objects. It\u2019s not necessary though that the data need to be a list.<\/p>\n<p>There are more functions in pagination.<\/p>\n<p><strong>Q.21 Explain password management in Django? In what format, Django stores passwords?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Django has various built-in tools to manage and store passwords. The way Django store passwords are encryption through various levels. Django uses the <strong>PBKDF2(Password Based Derivation Function 2)<\/strong> which is an industry-standard. The function results are also iterated over. Our passwords are finally stored in a way like this:<\/p>\n<p><strong>&lt;algorithm&gt;$&lt;iterations&gt;$&lt;salt&gt;$&lt;hash&gt;<\/strong><\/p>\n<p>This is the format in which Django stores a password. We specify the algorithm used before the dollar $ sign. The second part contains the number of iterations. The number of iterations over the algorithm is generally more than a million times. A random salt is included after that. The hash of the resulting password is the last part of the string.<\/p>\n<p>The default hash used by Django is SHA256. NIST recommends these standards. Django\u2019s password system is customizable. We can give it the customized algorithms but, in most cases, it\u2019s not preferred. Default implementations are what most websites ever need. The default security is already very high.<\/p>\n<p><strong>Q.22 How does Django handle weak passwords? How it implements password validation?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Password validation is another very useful feature in Django. People often resort to using weak passwords. To prevent them from doing so, Django provides a solution. Django\u2019s password validation system is a part of <strong>django.contrib.auth<\/strong> application.<\/p>\n<p>The Django has PASSWORD_VALIDATORS for this purpose. The password validators will check the passwords before saving it. The validators like it must contain 8 characters. They are the validators that check the data like validating all characters are not digits. There are even validators that check passwords among the very common ones. For example passwords like<strong> test123, hello123,<\/strong> etc raise validation error.<\/p>\n<p>We can even write our custom password validators in the case. The custom validators can even combine the user-information and test password. They can tell whether passwords are a combination of user-information or not.<\/p>\n<p>There are various ways. The main requirement by any validator is its description. The validator must provide a description to the user as to what is wrong with the password.<\/p>\n<p>Django handles password validation by the\u00a0<strong>AUTH_PASSWORD_VALIDATORS<\/strong> setting.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/default-validators.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74174\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/default-validators.png\" alt=\"default validators - Django inteview questions and answers\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/default-validators.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/default-validators-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/default-validators-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/default-validators-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/default-validators-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/default-validators-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>This is the default validators. You can add custom validators easily once adding them with a\u00a0<strong>password_validation<\/strong> class.<\/p>\n<p><strong>Q.23 Explain some built-in validators in Django.<\/strong><\/p>\n<p><strong>Ans.<\/strong> The validator in Django checks the value. When the input value doesn\u2019t meet certain constraints, Django raises <strong>ValidationError<\/strong>. <strong>is_valid()<\/strong> method handles ValidationError. The method then returns the value as <strong>False<\/strong>. Thus, we can ascertain whether the Form is providing valid data or not.<\/p>\n<p>There are various built-in validators. We can access them using<strong> django.core.validators<\/strong> module. This module has a collection of validators for different fields in Django.<\/p>\n<p>Some of the common validators we use include:<\/p>\n<p><strong>1. EmailValidator:<\/strong> This validator is the default one checking emails in models. This validator checks whether the input email is valid or not. If the email is not valid, it returns so with a message.<\/p>\n<p>An important attribute of email validation is <strong>whitelist<\/strong>. The whitelist attribute of EmailValidator class contains a list of domains. Django will accept these domains regardless if they follow the general email conventions. The default value of that list includes localhost.<\/p>\n<p><strong>2. URLValidator:<\/strong> The URLValidator ensures that the inserted value looks like a URL. Now, it has some set of regular expressions. If the string passes that it shall be a URL. It doesn\u2019t dial the URL in the browser actually accessing the link. If the validator doesn\u2019t return anything, we get an <strong>invalid<\/strong> code.<\/p>\n<p>The <strong>scheme<\/strong> parameter is important here. You can specify the scheme of URLs like <strong>http, https, FTP,<\/strong> etc. The accepted URLs shall have these schemes or protocols. This is the default value in the scheme parameter.<\/p>\n<p><strong>3. MaxValueValidator:<\/strong> As the name suggests, the maximum value that a field accepts. It checks the value, not length.<\/p>\n<p><strong>4. MinLengthValidator:<\/strong> This validator will check the length of the input. It raises ValidationError if the error exceeds a certain length.<\/p>\n<p>There are other validators too, as Django provides a lot. We can use various parameters according to use. Using Django validators is always better than using custom validators. They provide a standard validation and are well integrated with the rest of Django. Although, writing custom validators is as easy.<\/p>\n<p><strong>Q.24 What are the various steps involved in form validation in Django?<\/strong><\/p>\n<p><strong>Ans.<\/strong> The <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/django-forms-handling-and-validation\/\">form validation<\/a><\/strong><\/em> is a complex process comprising several steps. An end result is a form where all the data is either partially or fully clean. The form can also be completely wrong. Form validation is the process of validating data before performing any database operations.<\/p>\n<p>The steps involved in form validation include:<\/p>\n<ol>\n<li>The first method executed is <strong>to_python()<\/strong> method. It will convert the data into the appropriate Python type to perform validation.<\/li>\n<li>The second method is<strong> validate()<\/strong> method. It handles field-specific validation.<\/li>\n<li>The third method is <strong>run_validators()<\/strong> method. It calls all the field\u2019s validators. The collective errors are then returned by this method.<\/li>\n<\/ol>\n<p>These three methods are actually executed in order. The<strong> clean()<\/strong> method calls all three methods in the appropriate order. This is a form class method. It calls all three <strong>to_python(), validate()<\/strong> &amp; <strong>run_validators()<\/strong> methods. The comprised results and validation errors are returned.<\/p>\n<p>That\u2019s how we get a cleaned form or validated form.<\/p>\n<p><strong>Q.25 What are some good practices when writing custom validators? How do we raise ValidationErrors as recommended by Django?<\/strong><\/p>\n<p><strong>Ans. <em><a href=\"https:\/\/data-flair.training\/blogs\/django-exceptions-and-error-handling\/\">Raising errors in Django<\/a><\/em><\/strong> shows how well planned a web-developer can be. There are certain ways recommended by Django to raise ValidationErrors.<\/p>\n<ol>\n<li>The ValidationErrors shall be descriptive and easy to comprehend.<\/li>\n<li>The error code shall be a descriptive one.<\/li>\n<li>Django encourages the use of the <strong>params parameter<\/strong> when returning variables.<\/li>\n<li>The <strong>params<\/strong> argument shall contain mapped data. Dictionaries are used in these cases.<\/li>\n<li>We use the gettext function or <strong>(_(\u2018text\u2019))<\/strong> to enable translation.<\/li>\n<\/ol>\n<p><strong>Example Code:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/validation-error.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-74261 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/validation-error.jpg\" alt=\"validation error - django interview questions\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/validation-error.jpg 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/validation-error-150x80.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/validation-error-300x161.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/validation-error-768x412.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/validation-error-1024x549.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/validation-error-520x279.jpg 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>We have implemented all the concepts while raising validation error. A well thought validation error helps you better control the flow.<\/p>\n<p><strong>Q.26 What is the code layout for creating custom templates and filters?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Custom template tags are often used by programmers if the Django has a built-in tag. The custom template system is often defined in a different application. They are then made available to other <em><strong><a href=\"https:\/\/data-flair.training\/blogs\/create-django-templates\/\">Django templates<\/a><\/strong><\/em>.<\/p>\n<p>The code layout works like this.<\/p>\n<p><strong>1.<\/strong> Make a new app. Install that application in the Django project.<\/p>\n<p><strong>Code:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">python manage.py startapp dataflair<\/pre>\n<p><strong>2.<\/strong> Make a new directory <strong>templatetags<\/strong> at the same level as <strong>views.py, models.py,<\/strong> etc\u2026<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/templatetags.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74180\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/templatetags.png\" alt=\"templatetags - Django inteview questions and answers\" width=\"620\" height=\"361\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/templatetags.png 620w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/templatetags-150x87.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/templatetags-300x175.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/templatetags-520x303.png 520w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p><strong>3.<\/strong> Inside the <strong>templatetags<\/strong> directory, make a new file called <strong>__init__.py<\/strong><\/p>\n<p><strong>4.<\/strong> Then write your template tags or custom logic in a Python file. You can name it anything relevant to custom templates.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/custom-logic.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74183\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/custom-logic.png\" alt=\"custom logic - Django inteview questions and answers\" width=\"618\" height=\"447\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/custom-logic.png 618w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/custom-logic-150x108.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/custom-logic-300x217.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/custom-logic-520x376.png 520w\" sizes=\"auto, (max-width: 618px) 100vw, 618px\" \/><\/a><\/p>\n<p><strong>5.<\/strong> The custom templates can be used by other templates. We use <strong>{% load \u201cmodule-name\u201d %}<\/strong> to load the templates.<\/p>\n<p>The <strong>load<\/strong> tag will load all the custom templates defined in the file <strong>customDataFlair<\/strong>.<\/p>\n<p><strong>{% load customDataFlair %}<\/strong><\/p>\n<p>The template tags from the module will work if this is imported. There can be multiple load statements in the same template.<\/p>\n<p><strong>Q.27 What is the use of {autoescape} tag in DTL?<\/strong><\/p>\n<p><strong>Ans.<\/strong> HTML escaping is important and developers should keep that in mind while coding. The HTML escaping means escaping character like \u201c, &amp;, &lt;, &gt;. The browser engine can interpret these characters as HTML Mark-up. These can sometimes lead to glitches. Also, in the worst scenario, the user gets a broken site. To prevent that to happen, we use HTML escaping. The Django developers have come up with a solution.<\/p>\n<p>The solution to perform HTML escaping built-in is achieved by <strong>autoescape<\/strong> tag in DTL. The tag will identify the problem characters and replace them with escape characters. <strong>For example:<\/strong> \u201c represents <strong>&amp;quot;<\/strong>.\u00a0Other characters too have their escape sequences.<\/p>\n<p>The<strong> {% autoescape %}<\/strong> tag has one argument. The argument can be on or off. That indicated whether auto-escaping is on or off. The code resides between <strong>{% autoescape %}<\/strong> and<strong> {% endautoescape %}<\/strong> tags.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">{% autoescape on %}\r\n{{ dataflair.value }}\r\n{% endautoescape %}<\/pre>\n<p>This code will automatically look for any escape characters in <strong>dataflair.value<\/strong> variable. Then it will replace the escape characters if found.<\/p>\n<p>This function can help us achieve a valid and bugless HTML. Django encourages the use of autoescape character.<\/p>\n<p><strong>Q.28 What are formsets in Django?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Formsets provide a layer of abstraction to developers. They enable developers to work with multiple forms on the same page. The formsets follow the analogy of datagrid. Django provides various functions in the <a href=\"https:\/\/docs.djangoproject.com\/en\/3.0\/ref\/forms\/\">forms<\/a> module for this purpose.<\/p>\n<p>Suppose we have a class.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-formset-factory.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-74185\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-formset-factory.png\" alt=\"django formset factory - Django inteview questions and answers\" width=\"1920\" height=\"1030\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-formset-factory.png 1920w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-formset-factory-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-formset-factory-300x161.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-formset-factory-768x412.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-formset-factory-1024x549.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-formset-factory-520x279.png 520w\" sizes=\"auto, (max-width: 1920px) 100vw, 1920px\" \/><\/a><\/p>\n<p>If we want the user to create several objects at once, we use formsets. Django provides a class <strong>formset_factory<\/strong> for this purpose.<\/p>\n<p>We can make our form a formset as shown in the line 19.<\/p>\n<p>Now, we have made DjangoObjects a formset of <strong>DataFlairForm<\/strong> form. The <strong>formset_factory<\/strong> is a class present to help us in doing so.<\/p>\n<p>Now, we can make an instance of this variable. The instance of the article will provide us with a list of forms. The for loop can iterate over that list. We can get multiple forms on a single page using this. You can see the output on line 20.<\/p>\n<p><strong>Q.29 What are signals in Django?<\/strong><\/p>\n<p><strong>Ans.<\/strong> Signals are messages for applications. The signals are present to transmit the occurring of an event. They can notify other apps that a particular event has occurred. These are pretty useful when the applications exist which are interested in a mutual event.<\/p>\n<p>There are events like saving data in the database like deleting something from the database. There are various backend actions that are performed.<\/p>\n<p>Signals are core functionality and may not be used in every project. Still, it\u2019s nice to have an application that can help us use it.<\/p>\n<p>Django\u2019s signal is especially good as it transmits objects between applications. The native implementation is very efficient. There are various signals provided by Django like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">django.db.models.signals.pre_save\r\ndjango.db.models.signals.post_save\r\ndjango.db.models.signals.pre_delete\r\ndjango.db.models.signals.m2m_changed\r\ndjango.core.signals.request_started<\/pre>\n<p>These are the common ones. There are others as well.<\/p>\n<p><strong>Q.30 How do you listen to signals in Django?<\/strong><\/p>\n<p><strong>Ans.<\/strong> We use signals to transmit messages between applications. The messages will give notification of an event. The Signals package provides various functions to make signals.<\/p>\n<p>There are also functions to receive or listen to signals.<\/p>\n<p>To receive a signal we use Signal.connect() method in an app. The method has some important parameters.<\/p>\n<p>The signal will call receiver function when connect() function gets a message. The message signifies the occurrence of event. Django handles the signaling and activating of all these things.<\/p>\n<p>The connect() method takes 4 parameters:<\/p>\n<ol>\n<li>receiver<\/li>\n<li>sender<\/li>\n<li>weak<\/li>\n<li>dispatch_uid<\/li>\n<\/ol>\n<p>The receiver refers to a call back function. Django calls the receiver function when it gets the signal.<\/p>\n<p>The sender parameter takes a specific sender. A particular sender is set to receive signals from. The weak attribute takes two values, True or False. Django stores signals as weak references. Garbage collectors handle weak references. When the receiver function is local, it can be garbage collected. To prevent this, weak is set to false.<\/p>\n<p>The last parameter is a unique id. We use it to distinguish between duplicate signal receivers.<\/p>\n<p>Setting these parameters in a function will make the signal receiver. The receiver can then execute a function when an event occurs.<\/p>\n<h2>Summary<\/h2>\n<p>These were all the Django interview questions and answers. It covers the technical aspect and focuses on testing the advanced Django concepts of the candidate. We tried to explain the Python Django interview questions and answers in detail for a better understanding. So, practice them nicely.<\/p>\n<p><em><strong>Enhance your resume &amp; impress recruiters with the <a href=\"https:\/\/data-flair.training\/blogs\/django-project-news-aggregator-app\/\">Django Project on News Aggregator Web App<\/a><\/strong><\/em><\/p>\n<p>How was your experience of DataFlair&#8217;s Django Interview Questions and Answers? Do share in the comment section.<span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1252,&quot;href&quot;:&quot;https:\\\/\\\/pypi.org\\\/project\\\/django-model-utils&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20251010161836\\\/https:\\\/\\\/pypi.org\\\/project\\\/django-model-utils\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 04:24:32&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-18 09:20:08&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-27 23:13:00&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-07 14:11:08&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-12 10:21:23&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-01 17:32:05&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-06 03:47:40&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-03-12 19:06:50&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-02 01:53:12&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-04-30 09:14:59&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-05-14 05:40:12&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-20 05:36:00&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-25 00:52:22&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-25 00:52:22&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;},{&quot;id&quot;:1253,&quot;href&quot;:&quot;https:\\\/\\\/docs.djangoproject.com\\\/en\\\/3.0\\\/ref\\\/forms&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250918064203\\\/https:\\\/\\\/docs.djangoproject.com\\\/en\\\/3.0\\\/ref\\\/forms\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 04:24:37&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-18 09:20:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-27 23:13:02&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-07 14:11:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-12 10:21:24&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-06 03:49:35&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-12 19:07:47&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-02 01:54:01&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-04-30 09:16:46&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-02 11:30:58&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-20 05:36:01&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-25 00:52:22&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-06-25 00:52:22&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django Interview Questions and Answers After practicing the basic interview questions, it&#8217;s time to level up with Django interview questions for experienced professionals. These questions will help you with the technical interviews for a&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":74263,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19149],"tags":[21645,19565,21642,21644,21648,21646,21647,21643],"class_list":["post-74081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django","tag-advanced-django-interview-questions","tag-django-interview-questions","tag-django-interview-questions-and-answers","tag-django-interview-questions-for-experienced","tag-django-pagination","tag-latest-django-interview-questions-and-answers","tag-most-asked-django-interview-questions","tag-python-django-interview-questions"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Django Interview Questions and Answers - Get hired as Django developer - DataFlair<\/title>\n<meta name=\"description\" content=\"Django interview questions and answers - Get the preparation guide that will help you to crack your django interview and grab a job as django developer.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Django Interview Questions and Answers - Get hired as Django developer - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Django interview questions and answers - Get the preparation guide that will help you to crack your django interview and grab a job as django developer.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-01-04T05:24:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-21T06:49:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-interview-questions-and-answers.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"26 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Django Interview Questions and Answers - Get hired as Django developer - DataFlair","description":"Django interview questions and answers - Get the preparation guide that will help you to crack your django interview and grab a job as django developer.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/","og_locale":"en_US","og_type":"article","og_title":"Python Django Interview Questions and Answers - Get hired as Django developer - DataFlair","og_description":"Django interview questions and answers - Get the preparation guide that will help you to crack your django interview and grab a job as django developer.","og_url":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-01-04T05:24:18+00:00","article_modified_time":"2021-06-21T06:49:10+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-interview-questions-and-answers.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"26 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"Python Django Interview Questions and Answers &#8211; Get hired as Django developer","datePublished":"2020-01-04T05:24:18+00:00","dateModified":"2021-06-21T06:49:10+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/"},"wordCount":5204,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-interview-questions-and-answers.jpg","keywords":["advanced django interview questions","Django Interview Questions","django interview questions and answers","django interview questions for experienced","Django pagination","Latest Django Interview Questions and Answers","Most Asked Django Interview Questions","python django interview questions"],"articleSection":["Django Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/","url":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/","name":"Python Django Interview Questions and Answers - Get hired as Django developer - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-interview-questions-and-answers.jpg","datePublished":"2020-01-04T05:24:18+00:00","dateModified":"2021-06-21T06:49:10+00:00","description":"Django interview questions and answers - Get the preparation guide that will help you to crack your django interview and grab a job as django developer.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-interview-questions-and-answers.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/01\/django-interview-questions-and-answers.jpg","width":802,"height":420,"caption":"python django interview questions and answers"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/django-interview-questions-and-answers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Django Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/django\/"},{"@type":"ListItem","position":3,"name":"Python Django Interview Questions and Answers &#8211; Get hired as Django developer"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/74081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=74081"}],"version-history":[{"count":9,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/74081\/revisions"}],"predecessor-version":[{"id":74302,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/74081\/revisions\/74302"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/74263"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=74081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=74081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=74081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}