Today, we will be playing the Django MCQ quiz to test your intermediate to advanced conceptual knowledge of the framework. It will help you to evaluate your web development skills.

DataFlair has a complete series of Django quizzes. Make sure to bookmark them:

Let’s start the quiz.

Q.1 Suppose you want to count the number of books in Django.

books = Book.objects.all() Which implementation would be fastest? Database level Implementation – books.count() Python Implementation – len(books) Template Language Implementation – {{ books | length }} None of the above Correct! Wrong! The database level implementation will always be faster than the Python implementation. Python implementation is faster than the template’s implementation. To improve performance, mostly lower-level implementations are preferred. Q.2 From the previous question, suppose we want to implement the Query in the template. Choose one option which provides an optimal solution. {{ books | length }} {{ books.count }} {{ forloop.counter }} inside forloop over books Context Variable None of the above Correct! Wrong! The second implementation is the fastest. If you see, we are actually performing the books.count() in the template. It is even more optimal than the one with a database solution in the previous question. Q.3 What are some of the ways by which we can improve the site’s performance? Please select 5 correct answers Cached Sessions Using keep_lazy() decorators Database Optimizations Using Middlewares like ConditionalGetMiddleware Using Minification Techniques for HTML, CSS, JavaScript Caching the whole Site Using Template Language instead of Views layer Correct! Wrong! The techniques described here are all recommended by Django’s Documentation. The latter is not always effective. Caching the whole site won’t be of any use for large websites. It will only consume more memory. Using the template language for processing is not a good idea if the same can be achieved in view. The template language adds the templates header time which is extra in every sense. Q.4 Which of these variables are the settings for django.contib.staticfiles app? Please select 4 correct answers STATIC_URL STATIC_ROOT MEDIA_URL MEDIA_ROOT STATICFILES_DIRS STATICFILES_FINDERS Correct! Wrong! Often Django developers take media settings as the staticfiles app. The media settings are for a different Django app and middleware. The STATICFILES_DIRS and STATICFILES_FINDERS are used when the project contains multiple static files directory. The finders also become important when the website becomes large and has various static assets that change frequently. Q.5 Which of these commands is not a management command of staticfiles? python manage.py collectstatic python manage.py findstatic python manage.py runserver --nostatic python manage.py makemigrations Correct! Wrong! Q.6 What data types and objects are not accepted as an argument by Paginator class? Dictionary Tuple List Queryset None of the above Correct! Wrong! Dictionaries in Python don’t have a count(). The obvious answer could have been the dictionary but the dictionary does have the len method. Although using a dictionary in Paginator is not very fruitful as the methods are not made to iterate over a dictionary. Q.7 Suppose I have a Python class like this. Now, if we pass the obj in a context variable and add the template like this. {{ obj.topic }} Which value will the template engine return? topic attribute topic method throw an error Return empty string Correct! Wrong! The template system lookup works like this. Class attributes are searched before any method lookups. Therefore, the topic is returned rather than the method execution. Q.8 What are some valid forloop attributes of Django Template System? Please select 3 correct answers forloop.counter forloop.counter0 forloop.lastitem forloop.firstitem forloop.reverse forloop.revcounter Correct! Wrong! Q.9 Which of these is not a valid method or approach to perform URL resolution? Using Template {{ url : }} in template Using reverse() in View Functions Using get_absolute_url() None of the above Correct! Wrong! All three methods are valid and provide the absolute URL of the resources. Q.10 Which of the following Password Validators are not provided by default in Django? NumericPasswordValidator CommonPasswordValidator MinimumLengthValidator MaximumLengthValidator Correct! Wrong! The default password validators check for the minimum length of the password. They check whether the password is not completely numerical. Also, The CommonPasswordValidator will check whether the password matches with common passwords. The common passwords are a list of 20,000 passwords. The last one is not a default Password Validator. Q.11 Which of these are not built-in Validators in Django? MinLengthValidator EmailValidator ProhibitNullCharacterValidator None of the above Correct! Wrong! Q.12 Which of these is not a step in Form Validation? to_python() method validate() method run_validators() clear() clean() Correct! Wrong! Form validation is a complex process and comprises of various steps. Although, there is no tampering with data so there is no meaning of a clear method. The other methods are there to convert the data into Python data type. That data is then passed through custom and built-in validators. At last, we get cleaned data. Q.13 When we execute form.clean() on a form object and suppose at any point validation error is raised by a validator. What happens after the Validation Error occurs? The program stops executing and Django server stops. The clean() method stops validation and returns a list of validation errors. The clean() method continues execution and catches all the validation errors in that form. The clean() method catches the validation error and corrects it. Correct! Wrong! Q.14 Suppose I have a directory structure as shown. We want to extend the temp2.html and dataflair.html in temp3.html. Select all the options with the correct extend statement. The extend statement is written in temp3.html. Please select 2 correct answers {% extends “../../temp2.html” %} {% extends “../../dataflair.html” %} {% extends “dataflair.html” %} {% extends “../dataflair.html” %} {% extends “../temp2.html” %} Correct! Wrong! The file shall be present in the folder before extending it. In this case, the file is two-level above the directory. Thus, our answers are A and B.

This was all in the Django MCQ quiz. If you faced difficulty in answering the questions, don’t worry just revise the concepts from the Django tutorial series by DataFlair.

How many questions did you get correct in the Django MCQ quiz? Do share in the comment section.