Django Interview Questions
Django Interview Questions
Django Interview Questions
com
Django Interview Questions for experienced
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.
Django is a free and open source web application framework, written in Python. Django is named after Django
Reinhardt, Jazz guitarist from the 1930s to early 1950s who is one of the best guitarists of all time. Django was
mainly developed to handle the intensive design of the newsroom. You can even build high-quality web
applications using this. It adheres to the DRY principle and focuses completely on automating as much as
possible.
Django follows MVC -MVT architecture. MVT stand for Model View Template design Pattern which is little
bit different from MVC (Model View Controller ) Pattern.
Django is a high-level Python’s web framework which was designed for rapid development and clean realistic
design.
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.
Q8. What is the name of the Foundation which manages Django web framework?
Django web framework is managed and maintained by an independent and non-profit organization named
Django Software Foundation (DSF).
Q10. What are the advantages of using Django for web development?
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
No, Django is not a Content Management System (CMS). Instead, it is a Web framework and a programming
tool that helps you in building elegant websites.
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 control
the logic of the template.
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.
Middleware is a function that acts on or transforms a request/response before/after it passes through the view
layer (e.g. adding the user object to the request)
Some usage of middlewares in Django is:
Manage.py: It is an automatically created file in each Django project. It is a thin wrapper around the Django-
admin.py.
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.
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.
As Django is Python Framework, in order to install Django Python is required.Django comes with an inbuilt
lightweight web server that you can use for the testing purpose.If you are using Django on production Apache
with mod_wsgi is required.
PostgreSQL
MySQL
SQLite
Oracle
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
Follow the below steps to Install the development version of Django Framework.
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:
Source:https://docs.djangoproject.com/en/2.0/topics/install/
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:
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.
Iterators are used for traversing an object in Python which implements iterator protocol. It consists of two
methods __iter__() and next().
In Django, a good use of 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
In Django, a QuerySet can be evaluated in Iteration, Slicing, Pickling/Caching, repr(),len(), list() and bool().
By running below command on Terminal.You can check installed version of Django Framework.
py -m django --version
request.session['key'] = 'value'
del request.session['key']
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.
A mixin is a special kind of multiple inheritances in Python. There are two main situations where mixins are
used:
You can use {{ request.path }} and {{ request.get_full_path }} to get current page URI in Django template.
To create a constant in Django. Open your settings.py file and add a variable like MY_CONST =
“MY_VALUE”.
To use this constant in your views simply import setting like “Import settings in views.py” and use it as
settings.MY_CONST
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',
}
}
Django collectstatic command is used to collect all required static files from the STATIC_ROOT dir.
A Class-based view in Django provides an alternative way of implementing views as Python objects instead of
functions.
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.
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/"