Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

What are different model inheritance styles in the Django?

Answer»
  • Abstract BASE Class Inheritance: Used when you only need the PARENT class to HOLD information that you don’t want to write for each child model.
  • Multi-Table Model Inheritance:  Used when you are subclassing an existing model and need each model to have its own table in the database.
  • Proxy Model Inheritance:  Used when you want to retain the model's field while altering the python level functioning of the model.
2.

What is the difference between a project and an app in Django?

Answer»

In simple WORDS Project is the entire DJANGO APPLICATION and an app is a module inside the project that deals with one SPECIFIC use case. 
For eg, payment SYSTEM(app) in the eCommerce app(Project).

3.

What are Django URLs?

Answer»

URLs are one of the most important parts of a web application and Django provides you with an elegant way to design your own custom URLs with help of its module known as URLconf (URL Configuration). The basic functionality of this python module is to 
You can design your own URLs in Django in the way you like and then map them to the python function (View function). These URLs can be static as well as dynamic. These URLs as PRESENT in the urls.py where they are MATCHED with the EQUIVALENT view function. 

Basic SYNTAX:

from django.urls import pathfrom . import viewsurlpatterns = [ path('data/2020/', views.data_2020), path('data/<int:year>/', views.data_year)]
4.

What is Jinja templating?

Answer»

Jinja Templating is a very POPULAR templating engine for Python, the latest version is Jinja2. 

Some of its features are:

  • Sandbox Execution - This is a sandbox (or a protected) framework for automating the testing process
  • HTML ESCAPING - It PROVIDES automatic HTML Escaping as <, >, & CHARACTERS have special values in templates and if using a regular text, these symbols can lead to XSS Attacks which Jinja DEALS with automatically.
  • Template Inheritance
  • Generates HTML templates much faster than default engine
  • Easier to debug as compared to the default engine.
5.

What is django-admin and manage.py and explain its commands?

Answer»

django-admin is Django’s command-line utility for administrative TASKS. In addition to this, a manage.py FILE is also automatically created in each Django project. Not only does it perform the same purpose as the django-admin but it also sets the DJANGO_SETTINGS_MODULE environment variable to point to the project's settings.py file.

  • django-admin help - used to display usage information and a list of the commands provided by each application.
  • django-admin version - used to check your Django version.
  • django-admin check - used to inspect the entire Django project for common problems.
  • django-admin compilemessages - Compiles .po files created by makemessages to .mo files for use with the help of built-in gettext support.
  • django-admin createcachetable - Creates the cache tables for use in the database cache backend.
  • django-admin dbshell - Runs the command-line client for the database engine specified in your ENGINE setting(s), with the connection parameters (USER, PASSWORD, DB_NAME, USER etc.) specified settings file.
  • django-admin diffsettings - Shows the difference between the EXISTING settings file and Django’s default settings.
  • django-admin dumpdata - Used to the dumpdata from the database.
  • django-admin flush - Flush all values from the database and also re-executes any post-synchronization handlers specified in the code.
  • django-admin inspectdb - It generates django models from the existing database tables.
  • django-admin loaddata - loads the data into the database from the fixture file.
  • django-admin makemessages - Used for translation purpose and it generates a message file too.
  • django-admin makemigrations - Generates new migrations as per the changes detected to your models.
  • django-admin migrate - Executes SQL commands after which the database state with the current set of models and migrations are synchronized.
  • django-admin runserver - Starts a light-weight Web server on the local machine for development. The default server runs on port 8000 on the IP address 127.0.0.1. You can pass a custom IP address and port number explicitly if you want.
  • django-admin sendtestemail - This is used to confirm email sending through Django is working by sending a test email to the recipient(s) specified.
  • django-admin shell - Starts the Python interactive interpreter.
  • django-admin showmigrations - Shows all migrations present in the project.
  • django-admin sqlflush - Prints the SQL statements that would be executed for the flush command mentioned above.
  • django-admin sqlmigrate - Prints the SQL STATEMENT for the named migration.
  • django-admin sqlsequencereset - output the SQL queries for resetting sequences for the given app name(s).
  • django-admin squashmigrations - Squashes a range of migrations for a particular app_label.
  • django-admin startapp - Creates a new Django app for the given app name within the current directory or at the given destination.
  • django-admin startproject - Creates a new Django project directory structure for the given project name within the current directory or at the given destination.
  • django-admin test - Runs tests for all installed apps.
  • django-admin testserver - Runs a Django development server (which is also executed via the runserver command) using data from the given fixture(s).
  • django-admin changepassword - offers a method to change the user's password.
  • django-admin createsuperuser - Creates a user account with all permissions(also known as SUPERUSER account).
  • django-admin remove_stale_contenttypes - removes stale content types (from deleted models) in your database.
  • django-admin clearsessions - Can be used to clean out expired sessions or as a cron job.
6.

What is Django Rest Framework(DRF)?

Answer»

Django REST Framework is an open-source framework BASED upon Django which LETS you create RESTFUL APIs rapidly.

7.

Define static files and explain their uses?

Answer»

WEBSITES generally need to serve ADDITIONAL files such as IMAGES. JAVASCRIPT or CSS. In Django, these files are referred to as “STATIC files”, Apart from that Django provides django.contrib.staticfiles to manage these static files.

8.

What is Django ORM?

Answer»

This ORM (an ACRONYM for Object Relational Mapper) ENABLES us to interact with DATABASES in a more pythonic way like we can avoid writing RAW queries, it is possible to retrieve, save, delete and perform other operations over the database without ever writing any SQL query. It works as an abstraction layer between the models and the database.

9.

What are views in Django?

Answer»

A view function, or “view” for short, is SIMPLY a Python function that takes a web REQUEST and returns a web response. This response can be HTML contents of a web PAGE, or a redirect, or a 404 ERROR, or an XML document, or an image, etc. 

Example:

from django.http import HttpResponsedef sample_function(request): return HttpResponse(“Welcome to Django”)

There are TWO types of views:

  • Function-Based Views: In this, we import our view as a function.
  • Class-based Views: It’s an object-oriented approach.
10.

What are templates in Django or Django template language?

Answer»

Templates are an integral PART of the Django MVT architecture. They generally comprise HTML, CSS, and js in which DYNAMIC variables and information are embedded with the help of views. Some constructs are recognized and INTERPRETED by the template engine. The main ones are variables and tags.

A template is rendered with a context. Rendering just replaces variables with their VALUES, present in the context, and processes tags. Everything else remains as it is.

The syntax of the Django template language includes the following four constructs :

  • Variables
  • Tags
  • Filters
  • Comments

To read more about templates you can refer to this: https://docs.djangoproject.com/en/3.1/topics/templates/

11.

What are models in Django?

Answer»

A model in Django refers to a CLASS that MAPS to a database table or database collection. Each attribute of the Django model class represents a database field. They are defined in app/models.py

Example: 

from django.db import modelsclass SampleModel(models.Model):field1 = models.CharField(max_length = 50)field2 = models.IntegerField()class Meta:db_table = “sample_model”

Every model inherits from django.db.models.Model

Our example has 2 attributes (1 char and 1 integer field), those will be in the table fields.

The metaclass helps you set things like available permissions, singular and plural VERSIONS of the name, associated database table name, whether the model is abstract or not, etc.

To get more information about models you can refer here: https://docs.djangoproject.com/en/3.1/topics/db/models/.

12.

Explain the django project directory structure?

Answer»
  • manage.py - A command-line utility that allows you to interact with your Django project
  • __init__.py - An empty file that tells Python that the current directory should be considered as a Python package
  • settings.py - COMPRISES the configurations of the current project LIKE DB connections.
  • urls.py - All the URLs of the project are present here
  • wsgi.py - This is an entry POINT for your application which is used by the web servers to SERVE the project you have CREATED.
13.

Explain Django Architecture?

Answer»

Django follows the MVT (Model View Template) pattern which is BASED on the Model View Controller architecture. It’s slightly different from the MVC pattern as it maintains its own conventions, so, the controller is handled by the FRAMEWORK itself. The template is a presentation layer. It is an HTML file mixed with Django Template Language (DTL). The developer PROVIDES the model, the view, and the template then maps it to a URL, and finally, Django serves it to the user.