Top Django Interview Questions


This time, i had compiled some Django Interview Questions. 

This List might come handy to both Interviewer and Interviewee. 

  1. How do you create new project in Django ?
  2. How do you create new app in Django ?
  3. Explain MVT architecture of Django ?
  4. What are Middlewares ?
  5. How do you create custom Middleware ? What special methods are used to handle request/response/exception ?
  6. In what order does middlewares listed in are executed when request comes ? Top to bottom or bottom to up.
  7. In what order does middlewares listed in are executed when response goes ? Top to bottom or bottom to up.
  8. Explain Django ORM(Object-relational mapper) ?
  9. How does session framework works in Django ?
  10. What is CSRF ? How can you prevent csrf attack ?
  11. What are Django forms ?
  12. What are Django Models ?
  13. How do you migrate your tables in Django ?
  14. What are model manager and how do you write custom manager ?
  15. What are Django Templates ? How template inheritance works in Django ?
  16. What is the use of file ?
  17. What are signals ? How do you create custom signals in Django ?
  18. What are receiver functions in Django ?
  19. What are Django template tags ?
  20. How do you create custom template tags ?
  21. What are generic views ? How many types of generic views are there ?
  22. What’s the difference between select_related and prefetch_related in Django ORM?
  23. What is the use of context processors in Django ?
  24. What 3rd party Django apps you have used ?
  25. What are Python Virtual Environments ?



How to Rename Remote GIT branch

If you had named a git branch incorrectly and had pushed the branch to remote repository. Follow the steps listed below to rename the remote branch:

  1. Rename the local branch. 

    If you are on the branch then,

    git branch -m <new-name>

    If you are not on the branch then,

    git branch -m <old-name> <new-name>   

  2. Delete the old named remote branch and push new named local branch 

    git push origin :<old-name> <new-name>

    On success, this text will be displayed.

    – [deleted]

    * [new branch] ->

  3. Reset upstream branch for the local branch. 

    git push origin -u <new-name>

    Branch <new-name> set up to track remote branch from origin.
    Everything up-to-date


How to send messages using Python

In this tutorial, we will learn to send message using Python. We can use this approach in building new Python applications.

Tools we will be needing :

  1. Python – Version 2 or 3.
  2. Virtual Environment  – To create isolated Python environments
  3. Twilio Trial Account.
  4. Twilio Python helper library.

Twilio API :

We will be using Twilio Python helper library to send messages over the phone. To use Twilio library we will need to create twilio account. Signup for a free account

Twilio   Try Twilio Free.png

Once you had signed up on twilio, you will receive verification code on your registered phone number, input this code on twilio verification page and your account is set to use twilio services.

In Trial Mode, Twilio can be only used to send messages to a validated phone number. You will have to upgrade your account in-order to send messages to any phone number.

Now login into twilio and grab twilio account test credentials by click over here.

#Note : Copy your TEST ACCOUNT SID and TEST AUTHTOKEN, we will be using this in python script.

Install Python Dependency

As mentioned earlier that we will be using twilio helper library for our Python application. The library can be install using pip command.

We will start the basic Python Project by first creating virtual environment. Virtual environment are used to isolate different Python environments.

virtualenv sendMsg

Now activate this environment using,

source sendMsg/bin/activate

As we are ready with our virtual environment, go ahead and install twilio library.

pip install twilio

Sending Message from Python Script

Create a new python file i.e and enter the following code in the file.

# import the Twilio client from the dependency
from import Client

# put your twilio credentials here
account_sid = "{{your_account_sid}}" # get it from
auth_token = "{{your_authentication_code}}"

client = Client(account_sid, auth_token)

media_url = ""

response = client.messages.create(
    to= "{{enter_your_twillio_account_phone_number}}", #
    from_= "+15005550006", #+15005550006 will be used for trial account.
    body= "Hello, how you doing ?",
    media_url= media_url # if you need to attach multimedia to your message, else remove this parameter.

Now run the python script using


In a few seconds you should be able to see a message appear on your phone – note that messages can sometime take a little longer because we are using test credentials and it might in queued stage. Always check API response to know exact status.

That is everything needed to send messages to phone. Awesome results using few lines of Python code. This code can be added to any Python program to add message sending capabilities.

Balanced Parentheses Check | Python

Problem : 

Given a string of opening and closing parentheses, 
check whether it’s balanced. 

We have 3 types of parentheses: round brackets: (), square brackets:
[], and curly brackets: {}. 

Assume that the string doesn’t contain 
any other character than these, no spaces words or numbers. 

As a reminder, balanced parentheses require every opening
parenthesis to be closed in the reverse order opened.
For example ‘([])’ is balanced but ‘([)]’ is not.

You can assume the input string has no spaces.

Solution :

def check_balanced_string(s1):
	if len(s1)%2 != 0:
		return False

	opening = set('([{')

	matches = set([ ('{','}'), ('[',']'), ('(',')') ])

	stack = []
	for perm in s1:

		if perm in opening:

			if len(stack) == 0 :
				return False
			last_val = stack.pop()

			if (last_val,perm) not in matches:
				return False

	return len(stack) == 0

# '([])'
b = check_balanced_string('[()]')
print b

Find the largest continuous sum | Python

Problem :

Given an array of integers(positive and neg),
Find the largest continuous sum .

Solution : 

def large_cont_sum(arr):
	if len(arr) < 1 :
		return 0
	max_sum = current_sum = arr[0]

	for num in arr[1:]:

		current_sum = max(num,current_sum+num)
		max_sum = max(current_sum,max_sum)

	print max_sum

large_cont_sum([1,2,-1,3,4,10,10,-10,-1]) #29

Linked List Nth to Last Node | Python

Problem :

Function that takes a head node and an integer value n and 
then returns the nth to last node in the linked list.

Solution : 

class Node:

    def __init__(self, value):
        self.value = value
        self.nextnode  = None

    def nth_to_last_node(n, head):

	node = head

	nodes = []
	while node != None:
		node = node.nextnode

	return nodes[-n]

Recursive Binary Search | Python

Implement recursive binary search in Python

def recursive_binary_search(arr,ele):

	if len(arr) == 0:
		return False

		mid = len(arr)/2

		if arr[mid] == ele:
			return True
			if ele < arr[mid]:
				return recursive_binary_search(arr[:mid], ele)
				return recursive_binary_search(arr[mid+1:], ele)

d = recursive_binary_search([1,2,3,4,5],22)
print d