Home »
Python
Django MCQs
What is Django?
Django, pronounced "Jango," was named after the famous jazz musician Django Reinhardt. It is a free, open source system that was first made public in 2005. Django enables "rapid development and clean, pragmatic design." It is a high-level, open-source web framework written in Python that enables rapid development of secure and scalable web applications. It follows the Model-View-Controller (MVC) architectural pattern. Its main goal is to provide some set of tools and conventions to make complex web applications in a simpler manner.
Django Features
Key features of Django include:
- Object-Relational Mapping (ORM)
- URL Routing
- Template Engine
- Admin Interface
- Security
- Authentication and Authorization
- Internationalization and Localization
- Testing Framework
Django is famous because it is easy to use, scalable, and community support. It has been used to make everything from small personal projects to large-scale business apps.
Django MCQs with Answers
Django MCQs: This section contains multiple-choice questions and answers on Django. These MCQs are written for beginners as well as advanced, practice these MCQs to enhance and test the knowledge of Django.
List of Django MCQs
Here is the set of Top 35 Django multiple-choice questions with answers and explanations:
1. Django is a Python-based ____.
- web framework
- video creating tool
- analysis tool
- desktop development platform
Answer: A) web framework
Explanation:
Django is a Python-based web framework that is free and open-source. Django follows the model–template–views architectural pattern.
Discuss this question
2. Django is maintained by which organization/company?
- Oracle
- Microsoft Corporation
- Python Software Foundation
- Django Software Foundation
Answer: D) Django Software Foundation
Explanation:
Django is maintained by the Django Software Foundation, which is an independent organization established in the US as a 501 non-profit.
Discuss this question
3. What does MTV stand for in Django?
- model–template–views
- make–test–views
- model–template–verify
- mobile–template–verification
Answer: A) model–template–views
Explanation:
MTV stands for model–template–views is an architecture pattern that Django follows.
Discuss this question
4. Who is/are the original author(s) of Django?
- Pearu Peterson, Robert Kern, and Travis Oliphant
- Adrian Holovaty and Simon Willison
- Adrian Hallet and Simon Willison
- Larry Ellison, Bob Miner, and Ed Oates
Answer: B) Adrian Holovaty and Simon Willison
Explanation:
The original authors of Django are Adrian Holovaty and Simon Willison.
Discuss this question
5. Which command is used to create the first project in Django?
- django-admin startproject project_name
- django-admin startapp project_name
- django-admin create project_name
- django-admin createproject project_name
Answer: A) django-admin startproject project_name
Explanation:
The command to create the first project in Django (Run this command in the command prompt) is:
django-admin startproject project_name
Learn: Django | How to create first Web Application?
Discuss this question
6. By default, Django configuration uses which database?
- MySQL
- SQLite
- Oracle
- None of the above
Answer: B) SQLite
Explanation:
By default, Django configuration uses SQLite database.
Discuss this question
7. Which command is used to create an app in Django?
- py admin.py startapp-c app_name
- py manage.py startapp app_name
- py manage.py djangoapp app_name
- py manage.py createapp app_name
Answer: B) py manage.py startapp app_name
Explanation:
The command to create an app in Django is:
py manage.py startapp app_name
Discuss this question
8. Which is the correct command to start the Django development server on your system?
- py manage.py localhost
- py manage.py runatserver
- py manage.py createserver
- py manage.py runserver
Answer: D) py manage.py runserver
Explanation:
The correct command to start the Django development server on your system is:
py manage.py runserver
Discuss this question
9. Which file is not a part of the Django project content?
- settings.py
- manage.py
- templates.py
- py manage.py runserver
Answer: C) templates.py
Explanation:
File "templates.py" is not a part of the Django project content.
Discuss this question
10. Django is written in which language?
- C++
- Python
- AngularJS
- Asp.Net
Answer: B) Python
Explanation:
Django is a free and open-source web framework, it is written in Python programming language.
Discuss this question
11. Which Django functions are used to take http requests and return http responses?
- Django views
- Django request() and response()
- Django templates
- Both A and B
Answer: A) Django views
Explanation:
To take http requests and return http responses, Django views are used which are Python functions.
Discuss this question
12. In Django, data is created in ____.
- tables
- views
- templates
- objects
Answer: D) objects
Explanation:
In Django, data is created in objects.
Discuss this question
13. In Django, data is created in objects, what are these objects called ____.
- models
- views
- templates
- database
Answer: A) models
Explanation:
In Django, data is created in objects, these objects are called models.
Discuss this question
14. Which is the correct statement to import models in the Django project?
- from django import models
- from django.models import models
- from django.db import models
- None of the above
Answer: C) from django.db import models
Explanation:
The correct statement to import models in the Django project is:
from django.db import models
Discuss this question
15. Which is the correct statement to run migrate command in Django?
- run manage.py migrate
- py migrate
- py manage.py pymigrate
- py manage.py migrate
Answer: D) py manage.py migrate
Explanation:
The correct statement to run migrate command in Django:
from django.db import models
Discuss this question
16. Which command is used to create a Python shell?
- run shell
- py manage.py shell
- run py manage.py shell
- py manage.py djangoshell
Answer: B) py manage.py shell
Explanation:
To open a Python shell, run the below-given command:
py manage.py shell
Discuss this question
17. What is the correct syntax to use a variable in the Django template?
- {{ variable_name }}
- < variable_name >
- << variable_name >>
- " variable_name "
Answer: A) {{ variable_name }}
Explanation:
If you want to use a variable in the Django template, you need to follow the below-given syntax:
{{ variable_name }}
Let's suppose there is a variable named "email", and you want to use it inside a Django template. Then you have to write it inside the double curly opening and closing braces.
Discuss this question
18. Which Django file contains all the configuration of your Django installation?
- main.py
- setting.py
- djangosetting.py
- settings.py
Answer: D) settings.py
Explanation:
The settings.py file contains all the configurations of your Django installation.
Discuss this question
19. How you can turn off the debugging in Django's configuration file?
- DEBUG = false
- DEBUG = FALSE
- DEBUG = False
- DEBUGOFF = True
Answer: C) DEBUG = False
Explanation:
You can turn off the debugging by writing DEBUG = False in Django's configuration file.
Discuss this question
20. Which template tag is used to create variables directly in the Django template?
- {% with %}
- <% with %>
- << with >>
- {{ with }}
Answer: A) {% with %}
Explanation:
You can use {% with %} template tag inside the Django template to create variables directly. Consider the below example –
{% with name="Alvin" %}
<h1>Hey {{ name }}, Welcome!</h1>
{% endwith %}
The output of the following code will be –
Hey Alvin, Welcome!
Discuss this question
21. Which Django template tag is used to return the first not empty variable?
- empty
- firstempty
- firstemptyvar
- firstof
Answer: D) firstof
Explanation:
The firstof is a Django template tag that is used to return the first not empty variable. Consider the below syntax –
{% firstof variable_name_1 variable_name_2 variable_name_3 etc. %}
Discuss this question
22. How many parameters do render() function take in Django?
- 1
- 2
- 3
- 4
Answer: C) 3
Explanation:
Th render() function takes 3 parameters, which are request, template, and context_dictionary. Consider the below syntax –
render(request, template, {})
Discuss this question
23. Which is the correct import statement to use the render() function in Django?
- from django.shortcuts import render
- from django import render
- from django.requests import render
- from httpresponses import render
Answer: A) from django.shortcuts import render
Explanation:
The correct import statement to use the render() function in Django is:
from django.shortcuts import render
Discuss this question
24. Which Django tag is used to write comments?
- {% startcomment %} ... {% endcomment %}
- {% comment %} ... {% endcomment %}
- {% initcomment %} ... {% endcomment %}
- {% start %} ... {% end %}
Answer: B) {% comment %} ... {% endcomment %}
Explanation:
The Django tag comment is used to write comment. Here is the syntax of comment tag -
{% comment %} ... {% endcomment %}
Example:
<h1>My page</h1>
{% comment %}
<h1>Hello, world!</h1>
{% endcomment %}
Discuss this question
25. Except Django comment tag, what can be used to write smaller comments?
- # ..
- /* … *
- {{# ... #}}
- {# ... #}
Answer: D) {# ... #}
Explanation:
The {# ... #} tag can also be used to write smaller comments. Below is the syntax –
{# ... #}
Example:
<h1>Welcome {# Comment can be written here#}</h1>
Discuss this question
26. Which is the correct syntax to write comments in Django views?
- # ..
- /* … *
- {{# ... #}}
- {# ... #}
Answer: A) # ...
Explanation:
Django views are Python functions i.e., they are written in Python. Thus, the Python comment character (#) can be used to write comments in Django views. Consider the below example –
def testing_function(request):
templateObj = loader.get_template('template_student.html')
#context = {
# 'var1': 'John',
#}
return HttpResponse(templateObj.render())
Discuss this question
27. Which Django tag is used to include a template inside the current template?
- allow
- insert
- import
- include
Answer: D) include
Explanation:
The Django tag include is used to include a template inside the current template. Consider the below-given example in which we are importing a templated named "students.html" inside the current template –
{% include 'students.html' %}
Discuss this question
28. Which Django keyword is used to send variables into the template?
- send
- go
- export
- with
Answer: D) with
Explanation:
You can use the with keyword to send variables into the template. Consider the below-given example –
{% include "students.html" with me="Alvin Alex" class="B.Tech First Year" %}
Discuss this question
29. In Django QuertSet, which method is used to get all the records and fields of a given model?
- get()
- all()
- getall()
- bunch()
Answer: B) all()
Explanation:
In Django QuertSet, the all() method is used to get all the records and fields of a given model. Consider the below code statement in which we are getting all records –
dataObj = students.objects.all()
Where, "students" is model name.
Discuss this question
30. In Django QuertSet, which method is used to get each object as a Python dictionary, with the names and values as key/value pairs?
- values()
- dictionary()
- getvalues()
- None of the above
Answer: A) values()
Explanation:
In Django QuertSet, the values() method is used to get each object as a Python dictionary, with the names and values as key/value pairs. Consider the below code statement –
dataObj = students.objects.all().values()
Where, "students" is model name.
Discuss this question
31. In Django QuertSet, which method is used to get the specified column?
- column_values()
- columnvalues()
- values_list()
- column_list()
Answer: C) values_list()
Explanation:
In Django QuertSet, the values_list() method is used to get the specified column. Consider the below code statement –
dataObj = students.objects.values_list('class')
Where, "students" is the model name, and "class" is the column name.
Discuss this question
32. In Django QuertSet, which method is used to filter your search?
- filter()
- search()
- filter_all()
- filter_values()
Answer: A) filter()
Explanation:
In Django QuertSet, the filter() method is used to filter your search and returns the rows only that match the search query. Consider the below code statement –
dataObj = students.objects.filter(class='B.tech').values()
Where, "students" is the model name, and "class" is the column name. The above statement will return only matched rows.
Learn: Django – How to filter empty or NULL names in a QuerySet?
Discuss this question
33. In Django QuertSet, how to add two search queries?
- By using the AND keyword
- By using the OR keyword
- By using the + symbol
- By using the comma separator
Answer: D) By using the comma separator
Explanation:
In Django QuertSet, the filter() method is used to filter your search and returns the rows only that match the search query. If you want to add more than one query, you can place them by separating them with commas. Consider the below code statement –
dataObj = students.objects.filter(class='B.tech', lastName='Gupta').values()
Where, "students" is the model name, and "class" is the column name. The above statement will return only matched rows.
Discuss this question
34. Which method is used to sort a Django QuerySet?
- sort()
- sort_by()
- order_by()
- order()
Answer: C) order_by()
Explanation:
The order_by() method is used to sort a Django QuerySet. Consider the below code statement –
dataObj = students.objects.all().order_by('name').values()
Where, "students" is the model name, and "name" is the column name. The above statement will sort the QuerySet based on the names.
Discuss this question
35. Which Django tag is used to insert the current date and time?
- date
- now
- datetime
- currentdatetime
Answer: B) now
Explanation:
The Django tag now is used to insert the current date and/or time. Consider the below syntax –
{% now format %}
Example:
<h1>{% now "Y-m-d" %}</h1>
Output:
2023-07-17
Discuss this question