Mar

Django Interview Questions
- Pradeep Kumar
- 23rd Mar, 2023
- 588 Followers
Django Interview Questions
What is Django?
Django is a free and open-source web framework written in Python and Based on the model-view-template architectural pattern. Django web framework is a set of components that help you to develop websites earlier and easier.
While building a website, you always need a similar set of components: a way to handle user authentication (signing up, signing in, signing out), a management panel for your website.
Fortunately, other people long ago noticed that the same problem is faced by developers while making a website, so they teamed up and created frameworks that give you ready-made components to use and Django is one of them.
Quick Questions about Django
Quick Questions | Answers |
Django is | Web Framework |
Django is written in | Python Language |
Django is licensed under | 3-clause BSD |
Django is Developed by | Django Software Foundation |
Django is written by | Adrian Holovaty, Simon Willison |
Django is released | on 15 July 2005 |
Django is based on | model-template-view (MTV) architectural pattern |
Key Responsibilities of Django Developer
As a Django Developer, some of the key responsibilities you may be expected to have include:
- Designing and developing web applications using the Django framework.
- Creating and managing databases using tools like PostgreSQL, MySQL, or SQLite.
- Building and maintaining APIs for web services and integrations with other systems.
- Writing clean, efficient, and scalable code that meets industry standards.
- Developing and maintaining custom templates, themes, and plugins for Django-based applications.
- Collaborating with cross-functional teams, including product managers, designers, and other developers
- Testing and debugging applications to ensure they are error-free and meet client requirements.
- Deploying applications to production environments and managing server configurations.
- Providing ongoing support and maintenance for applications and resolving any issues that arise.
Django Interview Questions for Beginners
1) What do you understand by Django?
2) What does Of Django Field Class types do?
Following points are specified by the Django Field Class type: –
- It specifies the database column type.
- It also specifies the default HTML widget which is availed while we render the form field.
- The requirements of the minimal validation which is used in Django admin is also specified by the field class.
3) Does Django Follow Architectural pattern?
4) Clarify the architecture of Django?
Django follows MVC -MVT architecture. MVT stand for Model View Template design Pattern which is little bit different from MVC (Model View Controller ) Pattern.
5) Is Django stable or not?
6) Is Django a high level or low-level framework?
7) How does Django work?
Django can be broken into several components:
- Models.py file: This file defines your data model by extending your single code line into full database tables and add a pre-built administration section to manage content.
- Urls.py file: It uses a habitual expression to confine URL patterns for processing.
- Views.py file: It is the main part of Django. The actual processing happens in view.
8) What is the name of the Foundation which manages Django web framework?
9) Name the features available in Django web framework?
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
10) What are the advantages of using Django for web development?
- Auto-generated web admin is provided by Django to make website administration easy.
- Pre-packaged API is also available for common user tasks.
- Business logic can be separated from the HTML using this framework.
- You can even divide the code modules into logical groups so as to make it flexible for changing.
- Template system is being provided so as to define HTML template for your web page to avoid code duplication.
11) What is the process of creating a project in Django?
To start a project in Django, use the command $django-admin.py and then use the following command:Project
_init_.py
manage.py
settings.py
urls.py
12) Is Django a content management system (CMS)?
13) What does the Django templates contain?
14) What is the use of session framework in Django?
The session framework helps you in storing and retrieving arbitrary data on a per-site visitor basis. The data is stored on the server side and abstracts the receiving and sending of cookies. We can implement sessions through a piece of middleware.
15) How can you set up static files in Django?
1) Set STATIC_ROOT in settings.py
2) Run manage.py collect static
3) Set up a Static Files entry on the PythonAnywhere web tab
16) How to use file based sessions?
You have to set the SESSION_ENGINE settings to “django.contrib.sessions.backends.file” to use file-based session.
17) What is some typical usage of middlewares in Django?
Some usage of middlewares in Django is:
- It can be used for Session management,
- User authentication can be done with the help of this.
- It helps in Cross-site request forgery protection
- Content Gzipping, etc.
18) What is the usage of Django-admin.py and manage.py?
Django-admin.py: It is a Django’s command line utility for administrative tasks.
Manage.py: It is an automatically created file in each Django project. It is a thin wrapper around the Django-admin.py.
It has the following usage:
- It puts your project’s package on sys.path.
- It sets the DJANGO_SETTING_MODULE environment variable to points to your project’s setting.py file.
19) What are signals in Django ?
Signal are inbuilt utility in Django. They allow to execute some piece of code based on some action or event is occurred in framework something like a new user register, on delete of a record.
Below is the list of some inbuilt signal in Django.
- pre_save and post_save.
- pre_delete and post_delete
- pre_request and post_request
- pre_request and post_request
20) Is Django free?
21) Django is based on which design pattern.
Django closely follows the MVC (Model View Controller) design pattern, however, it does use its own logic in the implementation. Because the “C” is handled by the framework itself and most of the excitement in Django happens in models, templates, and views, Django is often referred to as an MTV framework. In the MTV development pattern:
- M stands for “Model,” the data access layer. This layer contains anything and everything about the data: how to access it, how to validate it, which behaviors it has, and the relationships between the data.
- T stands for “Template,” the presentation layer. This layer contains presentation-related decisions: how something should be displayed on a Web page or other type of document.
- V stands for “View,” the business logic layer. This layer contains the logic that accesses the model and defers to the appropriate template(s). You can think of it as the bridge between models and templates.
Further reading https://djangobook.com/model-view-controller-design-pattern/
22) When and who create Django?
23) List server requirement to install Django Framework.
24) List the database backends supported by Django Framework?
Django officially supports four database backends, they are
- PostgreSQL
- MySQL
- SQLite
- Oracle
In addition to these, you can also use following 3rd parties
- SAP SQL Anywhere
- IBM DB2
- Microsoft SQL Server
- Firebird
- ODBC
25) What is recommended way to install Django?
Installing using pip is the recommended way to install Django Framework. Below are the steps to install official release of Django with pip
- Install pip.
- Configure virtualenv and virtualenvwrapper
- Once virtual environment is created and activated, enter the command pip install Django to install Django
26) How to install the development version of Django
Follow the below steps to Install the development version of Django Framework.
- Check out Django’s main development branch
$ git clone https://github.com/django/django.git
- Make sure that the Python interpreter can load Django’s code. The most convenient way to do this is to use virtualenv, virtualenvwrapper, and pip.
- After setting up and activating the virtualenv, run the following command:
$ pip install -e django/
Source:https://docs.djangoproject.com/en/2.0/topics/install/
27) Where are Django migrations stored?
You can think Django Migrations as version control system for your database/Model. It keeps track of changes done in your application Models/Table like adding a field, deleting a model, etc. Migrations in Django are stored as an on-disk format, referred to here as “migration files”. These files are actually just normal Python files with an agreed-upon object layout, written in a declarative style. A basic migration file looks like this:
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [('migrations', '0001_initial')]
operations = [
migrations.DeleteModel('Tribble'),
migrations.AddField('Author', 'rating', models.IntegerField(default=0)),
]
Further Reading https://docs.djangoproject.com/en/2.0/topics/migrations/
28) How a request is processed in Django?
In Django whenever a request is made by a user, it goes through the following steps:
- Django determines the root URLconf module to use. Ordinarily, this is the value of the ROOT_URLCONF setting, but if the incoming HttpRequest object has a urlconf attribute (set by middleware), its value will be used in place of the ROOT_URLCONF setting.
- Django loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.urls.path() and/or django.urls.re_path() instances.
- Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.
- Once one of the URL patterns matches, Django imports and calls the given view, which is a simple Python function (or a class-based view). The view gets passed the following arguments:
- An instance of HttpRequest.
- If the matched URL pattern returned no named groups, then the matches from the regular expression are provided as positional arguments.
- The keyword arguments are made up of any named parts matched by the path expression, overridden by any arguments specified in the optional kwargs argument to django.urls.path() or django.urls.re_path().
- If no URL pattern matches, or if an exception is raised during any point in this process, Django invokes an appropriate error-handling view.
29) When to use the iterator in Django ORM?
Iterators are used for traversing an object in Python which implements an iterator protocol. It consists of two methods __iter__() and next().
In Django, good use of an iterator is when you are processing results that take up a large amount of available memory (lots of small objects or fewer large objects).
For more clarification please read when to use and when to not use iterator() in the Python Django ORM
https://stackoverflow.com/questions/12681653/when-to-use-or-not-use-iterator-in-the-django-orm
30) When QuerySets are evaluated in Django?
31) How to check installed version of Django?
By running below command on Terminal.You can check installed version of Django Framework.
py -m django --version
32) How to set/unset session in Django?
Setting Session in Django
request.session['key'] = 'value'
Unset Session in Django
del request.session['key']
33) What is a context in Django?
In Django Context is a dictionary with variable names in the form of key and value like {varible1: 101, varible2: 102},when we pass this context to the template render method, {{ varible1 }} would be replaced with 101 and {{ varible2 }} with 102 in your template.
34) Explain mixins in Django.
A mixin is a special kind of multiple inheritances in Python. There are two main situations where mixins are used:
- You want to provide a lot of optional features for a class.
- You want to use one particular feature in a lot of different classes.
Read More from https://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful
35) How to get current page URI in Django template.
You can use {{ request.path }} and {{ request.get_full_path }} to get current page URI in Django template.
36) List some popular websites built on Django framework?
- Disqus
- Bitbucket
- Mozilla Firefox
- NASA
- Onion
- The Washington Post
- Eventbrite
- Mahalo
37) How to create an Constant in Django.
To use this constant in your views simply import setting like “Import settings in views.py” and use it as
settings.MY_CONST
38) How to Configure Database in Django.
The steps to configure Database in Django are as follows:
- Open up the settings.py file in your IDE
- Locate the database's setting and configure it as shown below.
DATABASES = { 'Default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'crmeasyDB', 'USER': 'postgres', 'PASSWORD': 'warning this is where you need to put your password', 'HOST': '/tmp', 'PORT': '5432', } }
39) List type of relationships/ asociations supported by Django.
The list of relationships supported by Django are:
- One to Many relationships
- Many to Many relationships
- One to One relationships
- Reverse relationships – related_name, related_query_name and symmetrical
40) What django collectstatic command does?
Django collectstatic command is used to collect all required static files from the STATIC_ROOT dir.
41) What are Class-based views in Django
A Class-based view in Django provides an alternative way of implementing views as Python objects instead of functions.
42) What constitutes Django templates?
Template can create formats like XML,HTML and CSV(which are text-based formats). In general terms template is a simple text file. It is made up of variables that will later be replaced by values after the template is evaluated and has tags which will control template’s logic.
43) List some caching strategies that you know in Django!
Few caching strategies that are available in Django are as follows:
- File system caching
- In-memory caching
- Using Memcached
- Database caching
44) List type of inheritance supported in Django?
Django supports 3 types of inheritance. They are
- Abstract base classes
- Multi-table Inheritance
- Proxy models
45) What is Static_root in Django?
STATIC_ROOT is the absolute path to the directory from where Django collectstatic will static files for deployment.
STATIC_ROOT example:
STATIC_ROOT="/var/www/project_dir/static/"
Leave A Comment :
Valid name is required.
Valid name is required.
Valid email id is required.