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:

  1. Object-Relational Mapping (ORM)
  2. URL Routing
  3. Template Engine
  4. Admin Interface
  5. Security
  6. Authentication and Authorization
  7. Internationalization and Localization
  8. 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 ____.

  1. web framework
  2. video creating tool
  3. analysis tool
  4. 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?

  1. Oracle
  2. Microsoft Corporation
  3. Python Software Foundation
  4. 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?

  1. model–template–views
  2. make–test–views
  3. model–template–verify
  4. 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?

  1. Pearu Peterson, Robert Kern, and Travis Oliphant
  2. Adrian Holovaty and Simon Willison
  3. Adrian Hallet and Simon Willison
  4. 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?

  1. django-admin startproject project_name
  2. django-admin startapp project_name
  3. django-admin create project_name
  4. 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?

  1. MySQL
  2. SQLite
  3. Oracle
  4. 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?

  1. py admin.py startapp-c app_name
  2. py manage.py startapp app_name
  3. py manage.py djangoapp app_name
  4. 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?

  1. py manage.py localhost
  2. py manage.py runatserver
  3. py manage.py createserver
  4. 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?

  1. settings.py
  2. manage.py
  3. templates.py
  4. 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?

  1. C++
  2. Python
  3. AngularJS
  4. 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?

  1. Django views
  2. Django request() and response()
  3. Django templates
  4. 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 ____.

  1. tables
  2. views
  3. templates
  4. 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 ____.

  1. models
  2. views
  3. templates
  4. 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?

  1. from django import models
  2. from django.models import models
  3. from django.db import models
  4. 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?

  1. run manage.py migrate
  2. py migrate
  3. py manage.py pymigrate
  4. 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?

  1. run shell
  2. py manage.py shell
  3. run py manage.py shell
  4. 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?

  1. {{ variable_name }}
  2. < variable_name >
  3. << variable_name >>
  4. " 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?

  1. main.py
  2. setting.py
  3. djangosetting.py
  4. 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?

  1. DEBUG = false
  2. DEBUG = FALSE
  3. DEBUG = False
  4. 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?

  1. {% with %}
  2. <% with %>
  3. << with >>
  4. {{ 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?

  1. empty
  2. firstempty
  3. firstemptyvar
  4. 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. 1
  2. 2
  3. 3
  4. 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?

  1. from django.shortcuts import render
  2. from django import render
  3. from django.requests import render
  4. 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?

  1. {% startcomment %} ... {% endcomment %}
  2. {% comment %} ... {% endcomment %}
  3. {% initcomment %} ... {% endcomment %}
  4. {% 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?

  1. # ..
  2. /* … *
  3. {{# ... #}}
  4. {# ... #}

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?

  1. # ..
  2. /* … *
  3. {{# ... #}}
  4. {# ... #}

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?

  1. allow
  2. insert
  3. import
  4. 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?

  1. send
  2. go
  3. export
  4. 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?

  1. get()
  2. all()
  3. getall()
  4. 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?

  1. values()
  2. dictionary()
  3. getvalues()
  4. 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?

  1. column_values()
  2. columnvalues()
  3. values_list()
  4. 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?

  1. filter()
  2. search()
  3. filter_all()
  4. 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?

  1. By using the AND keyword
  2. By using the OR keyword
  3. By using the + symbol
  4. 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?

  1. sort()
  2. sort_by()
  3. order_by()
  4. 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?

  1. date
  2. now
  3. datetime
  4. 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


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.