Django Interview Questions – Set 02

Explain Django.

Django is a free and open source web application framework, written in Python. It is a server-side web framework that provides rapid development of secure and maintainable websites.

What are the two important parameters in signals?

Two important parameters in signals are:

Receiver: It specifies the callback function which connected to the signal.
Sender: It specifies a particular sender from where a signal is received.

Which foundation manages the Django web framework?

Django web framework is managed and maintained by an independent and non-profit organization named Django Software Foundation (DSF). The primary foundation goal is to promote, support, and advance the Django Web framework.

How can you set up static files in Django?

There are three main things required to 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

What does Django mean?

Django is named after Django Reinhardt, a gypsy jazz guitarist from the 1930s to early 1950s who is known as one of the best guitarists of all time.

How to handle URLs in Django?

To handle URL, django.urls module is used by the Django framework.

Let’s open the file urls.py of the project and see the what it looks like:

// urls.py

from django.contrib import admin
from django.urls import path

urlpatterns = [
path(‘admin/’, admin.site.urls),
]
See, Django already has mentioned a URL here for the admin. The path function takes the first argument as a route of string or regex type.

The view argument is a view function which is used to return a response (template) to the user.

The django.urls module contains various functions, path(route,view,kwargs,name) is one of those which is used to map the URL and call the specified view.

Is Django stable?

Yes, Django is quite stable. Many companies like Disqus, Instagram, Pinterest, and Mozilla have been using Django for many years.

What is some typical usage of middlewares in Django?

Some usage of middlewares in Django is:

  • Session management,
  • Use authentication
  • Cross-site request forgery protection
  • Content Gzipping

Explain Django architecture.

Django follows MVT (Model View Template) pattern. It is slightly different from MVC.

Model: It is the data access layer. It contains everything about the data, i.e., how to access it, how to validate it, its behaviors and the relationships between the data.

Let’s see an example. We are creating a model Employee who has two fields first_name and last_name.

from django.db import models

class Employee(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
View: It is the business logic layer. This layer contains the logic that accesses the model and defers to the appropriate template. It is like a bridge between the model and the template.

import datetime
# Create your views here.
from django.http import HttpResponse
def index(request):
now = datetime.datetime.now()
html = ”

Now time is %s.

” % now
return HttpResponse(html) # rendering the template in HttpResponse
Template: It is a presentation layer. This layer contains presentation-related decisions, i.e., how something should be displayed on a Web page or other type of document.

To configure the template system, we have to provide some entries in settings.py file.

TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: [os.path.join(BASE_DIR,’templates’)],
‘APP_DIRS’: True,
‘OPTIONS’: {
‘context_processors’: [
‘django.template.context_processors.debug’,
‘django.template.context_processors.request’,
‘django.contrib.auth.context_processors.auth’,
‘django.contrib.messages.context_processors.messages’,
],
},
},
]

What is Django Exception?

An exception is an abnormal event that leads to program failure. To deal with this situation, Django uses its exception classes and supports all core Python exceptions as well. Django core exceptions classes are defined in django.core.exceptions module.