Django Interview Questions and Answers

Q.1.What is Django?
A.Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Q.2. Explain the architecture of Django?
A.Django is based on MVT architecture. It contains the following layers:Models: It describes the database schema and data structure.Views: The view layer is a user interface. It controls what a user sees, the view retrieves data from appropriate models and execute any calculation made to the data and pass it to the template.Templates: It determines how the user sees it. It describes how the data received from the views should be changed or formatted for display on the page.

Controller: Controller is the heart of the system. It handles requests and responses, setting up database connections and loading add-ons. It specifies the Django framework and URL parsing.

Q.3.What are the features available in Django web framework?
A. Features available in Django web framework are:Admin Interface (CRUD)
Templating
Form handling
Internationalization
Session, user management, role-based permissions
Object-relational mapping (ORM)
Testing Framework
Fantastic Documentation
Q.4.How to create a project in Django?
A.To start a project in Django, use the command $django-admin.py and then use the following command:Project_init_.pymanage.py

settings.py

urls.py

Q.5.What are the inheritance styles in Django?
A.There are three possible inheritance styles in Django:1. Abstract base classes: This style is used when you only want parent?s class to hold information that you don’t want to type out for each child model.2. Multi-table Inheritance: This style is used if you are sub-classing an existing model and need each model to have its own database table.3. Proxy models: This style is used, if you only want to modify the Python level behavior of the model, without changing the model’s fields.
Q.6.What does the Django templates contain?
A. A template is a simple text file. It can create any text-based format like XML, CSV, HTML, etc. A template contains variables that get replaced with values when the template is evaluated and tags (%tag%) that controls the logic of the template.
Q.7.What is the command to start Django’s built-in development server?
A. manage.py runserver is the command to start django’s server.
Q.8.What are ‘Models’?
A. Models are a single and definitive source for information about your data. It consists of all the essential fields and behaviors of the data you have stored. Often, each model will map to a single specific database table.In Django, models serve as the abstraction layer that is used for structuring and manipulating your data. Django models are a subclass of the django.db.models.Model class and the attributes in the models represent database fields.
Q.9.What are ‘views’?
A. Django views serve the purpose of encapsulation. They encapsulate the logic liable for processing a user’s request and for returning
the response back to the user. Views in Django either return an HttpResponse or raise an exception such as Http404. HttpResponse contains the objects that consist of the content that is to be rendered to the user. Views can also be used to perform tasks such as read records from the database, delegate to the templates, generate a PDF file, etc..
Q.10.When can you use iterators in Django ORM?
A. Iterators in Python are basically containers that consist of a countable number of elements. Any object that is an iterator implements two methods which are, the __init__() and the __next__() methods. When you are making use of iterators in Django, the best situation to do it is when you have to process results that will require a large amount of memory space. To do this, you can make use of the iterator() method which basically evaluates a QuerySet and returns the corresponding iterator over the results.
Q.10.Why Django should be used for web-development?
A. It allows you to divide code modules into logical groups to make it flexible to change
To ease the website administration, it provides auto-generated web admin
It provides pre-packaged API for common user tasks
It gives you template system to define HTML template for your web page to avoid code duplication
It enables you to define what URL be for a given function
It enables you to separate business logic from the HTML
Everything is in python
Q.12.What are the disadvantages of Django?
A.  Following is the list of disadvantages of Django:Django’ modules are bulky.
It is completely based on Django ORM.
Components are deployed together.
You must know the full system to work with it.
Q.13. What is Django Admin Interface?
A. Django comes with a fully customizable in-built admin interface, which lets us see and make changes to all the data in the database of registered apps and models. To use a database table with the admin interface, we need to register the model in the admin.py file.
Q.14. Explain Django’s Request/Response Cycle.
A. In the Request/Response Cycle, first, a request is received by the Django server. Then, the server looks for a matching URL in the urlpatterns defined for the project. If no matching URL is found, then a response with 404 status code is returned. If a URL matches, then the corresponding code in the view file associated with the URL is executed to build and send a response.
Q.15. What are migrations in Django?
A. A migration in Django is a Python file that contains changes we make to our models so that they can be converted into a database schema in our DBMS. So, instead of manually making changes to our database schema by writing queries in our DBMS shell, we can just make changes to our models. Then, we can use Django to generate migrations from those model changes and run those migrations to make changes to our database schema.
Q.16. What is a Meta Class in Django?
A. A Meta class is simply an inner class that provides metadata about the outer class in Django. It defines such things as available permissions, associated database table name, singular and plural versions of the name, etc.