Django MCQs

Django MCQs

These Django multiple-choice questions and their answers will help you strengthen your grip on the subject of Django. You can prepare for an upcoming exam or job interview with these Django MCQs.
So scroll down and start answering.

1: What is the command to start Django's built-in development server?

A. --start

B. startserver --dev

C. run

D. runserver

E. --run

2: What is the definition of a Django Project?

A.   A collection of configuration files and individual apps that form a web site.

B.   A fork of the official Django repo.

C.   A web site that uses the Django framework.

D.   A specific piece of functionality that can be used in multiple Django apps.

3: What is the Django shortcut method to more easily render an html response?

A.   render_to_html

B.   response_render

C.   render

D.   render_to_response

4: What is the correct syntax for including a class based view in a URLconf?

A.   (r'^pattern/$', YourView.as_view()),

B.   (r'^pattern/$', YourView()),

C.   (r'^pattern/$', YourView),

D.   (r'^pattern/$', YourView.init()),

5: What is the definition of a good Django app?

A.   A good Django app is a fully functioning website that has 100% test coverage.

B.   A good Django app provides a small, specific piece of functionality that can be used in any number of Django projects.

C.   A good Django app is highly customized and cannot be used in multiple projects.

6: What is the purpose of

A.   To sync the database schema

B.   To configure settings for an app

C.   To set the date and time on the server

D.   To configure settings for the Django project

7: Django is written using what programming language?

A.   Python

B.   Ruby

C.   PHP

D.   Javascript

E.   Java

8: What does a file do in Django?

A.   This file contains site deployment data such as server names and ports.

B.   It contains a site map of Django-approved URLs.

C.   You run this file when you get obscure 404 Not Found errors in your server logs.

D.   It contains URL matching patterns and their corresponding view methods.

E.   This file provides an up to date list of how-to URLs for learning Django more easily.

9: What is the command to run Django's development server on port 8080 on IP address

A. --dev

B. --run 8080

C. runserver

D. run

E. runserver

10: After you make a new 'app' in your existing Django project, how do you get Django to notice it?

A.   In, add the app to the PROJECT_APPS variable.

B.   In, add the new app to the INSTALLED_APPS variable.

C.   Run the ` validate` command, and then start a new shell.

D.   No additional action is required, Django notices new apps automatically.

E.   Run the ` syncdb` command.

11: What is the Django command to start a new app named 'users' in an existing project?

A. startapp users

B. --startapp users

C. --newapp users

D. newapp users

E. start users

12: What is the most easiest, fastest, and most stable deployment choice in most cases with Django?

A.   AJP

B.   FastCGI


D.   mod_wsgi

13: How do you define a 'name' field in a Django model with a maximum length of 255 characters?

A.   model = CharField(max_length=255)

B.   name = models.CharField(max_len=255)

C.   name = models.CharField(max_length=255)

D.   name = model.StringField(max_length=auto)

E.   model.CharField(max_length=255)

14: What preferred method do you add to a Django model to get a better string representation of the model in the Django admin?

A.   to_s( self )

B.   __utf_8__

C.   __translate__

D.   __unicode__

15: How do you exclude a specific field from a ModelForm?

A.   Set the field to hidden

B.   Create a new Form, don't use a ModelForm

C.   You can not do this

D.   Use the exclude parameter in the Meta class in your form

16: What is ModelForm used for?

A.   To define a form based on an existing model

B.   To model an input form for a template

C.   To specify rules for correct form when writing Django code

17: Assuming you have a Django model named 'User', how do you define a foreign key field for this model in another model?

A.   user = models.ForeignKey(User)

B.   models.ForeignKey( self, User )

C.   user = models.IntegerKey(User)

D.   model = new ForeignKey(User)

18: Assuming you've imported the proper Django model file, how do you add a 'User' model to the Django admin?

A. self, User )

B. Admin )

C.   admin.register( Users )

D. Admin )

E. User )

19: What happens if MyObject.objects.get() is called with parameters that do not match an existing item in the database?

A.   The object is created and returned.

B.   The Http404 exception is raised.

C.   The DatabaseError exception is raised.

D.   The MyObject.DoesNotExist exception is raised.

20: How to make django timezone-aware?

A.   in, import timezone

B.   In USE_TZ=True

C.   in, import tz

D.   In USE_L10N=True

E.   in, import timezone

21: What is the command to start a new Django project called 'myproject'?

A. --start myproject

B. --new myproject

C. startproject myproject

D. startproject myproject

E. new myproject

22: In Django how would you retrieve all the 'User' records from a given database?

A.   User.all_records()

B.   Users.objects.all()

C.   User.objects

D.   User.object.all()

E.   User.objects.all()

23: A set of helpful applications to use within your Django projects is included in the official distribution. This module is called what?

A.   django.helpers

B.   django.extras

C.   django.contrib

D.   django.utilities

E.   django.ponies

24: What does the Django command ` shell` do?

A.   Starts a command line in whatever $SHELL your environment uses.

B.   Starts a Django command prompt with your Python environment pre-loaded.

C.   Loads a Python command prompt you can use to sync your database schema remotely.

D.   Loads a special Pythonic version of the Bash shell.

E.   Starts a Python command prompt with your Django environment pre-loaded.

25: How can you define additional behavior and characteristics of a Django class?

A.   def __init__():

B.   def Meta():

C.   class Meta:

D.   def setUp():

E.   class __init__:

26: Where is pre_save signal in Django

A.   There is no pre_save signal

B.   from django.db.models import pre_save

C.   from django.db.models.signal import pre_save

D.   from django.db.models.signals import pre_save

27: What is Dijit?

A.   Dijit is Dojo's git interface.

B.   Dijit is Dojo’s UI Library.

C.   Dijit is Dojo's AMD loader.

D.   Dijit is Dojo's interface to the jQuery effects functions.

E.   Dijit is Dojo's math library.

28: What does the Django command ` validate` do?

A.   Checks for errors in your models.

B.   Checks for errors in your views.

C.   Checks for errors in your file.

D.   Checks for errors in your templates.

E.   Checks for errors in your controllers.

29: What is the correct way to include django's admin urls? from django.contrib import admin') from django.conf.urls import patterns, include, url urlpatterns = patterns('', ______________________ )

A.   url(r'^admin/', include( ),

B.   url(r'^admin/', admin.as_view(), name='admin ),

C.   url(r'^admin/', include(admin) ),

D.   url(r'^admin/', admin.urls ),

E.   admin.autodiscover()

30: What is the purpose of the STATIC_ROOT setting?

A.   Defines the URL prefix where static files will be served from .

B.   Defines the location where all static files will be copied by the 'collectstatic' management command, to be served by the production webserver.

C.   Defines the location for serving user uploaded files.

D.   A project's static assets should be stored here to be served by the development server.

31: Given the Python data: mydata = [ [ 0, 'Fred' ], [ 1, 'Wilma' ] ] How do you access the data in a Django template?

A.   {% for d in mydata -%} <p><a href="/users/{{ d.0 }}/">{{ d.1 }}</a></p> {% end -%}

B.   {% for d in mydata %} <p><a href="/users/{{ d.0 }}/">{{ d.1 }}</a></p> {% endfor %}

C.   {% mydata.each |d| %} <p><a href="/users/{{ d.1 }}/">{{ d.2 }}</a></p> {% end %}

D.   {{ for d in mydata }} <p><a href="/users/{{ d[0] }}/">{{ d[1] }}</a></p> {{ endfor }}

E.   {% for d in mydata %} <p><a href="/users/{% d.0 %}/">{% d.1 %}</a></p> {% endfor %}

32: Given an IntegerField named 'widgets' in the Django model 'User' , how do you calculate the average number of widgets per user?

A.   Widget.objects.all().aggregate( Avg( 'users' ) )

B.   User.objects.all().aggregate( Avg( 'widgets' ) )

C.   User.objects.avg( 'widgets' )

D.   User.objects.all().aggregate( Sum( 'widgets' ) )

E.   User.all().aggregate( Avg( 'widgets' ) ).count()

33: Given a model named 'User' that contains a DateTime field named 'last_login', how do you query for users that have never logged in?

A.   User.objects.filter( last_login__isnull=True )

B.   User.objects.filter( last_login=Null )

C.   User.objects.filter( last_login=Never )

D.   User.objects.filter( last_login__null=True )

E.   User.objects.filter( last_login__isnull=False )

34: What is the Django command to view a database schema of an existing (or legacy) database?

A. inspect

B. inspectdb

C. inspect

D. legacydb

E. schemadump

35: Given a model named 'User' that contains a field named 'email', how do you perform a case-insensitive exact match for the email ''?

A.   User.objects.filter( email__iexact='' )

B.   User.objects.filter( email__matches='' )

C.   User.objects.filter( email__exact='' )

D.   User.objects.filter( email__contains='' )

E.   User.objects.filter( email__icontains='' )

36: What command compile Django's translation files?

A.   ./ compilemessages

B.   ./ compiletranslation

C.   ./ translate_files

D.   ./ compilei18n

E.   ./ i18n_update

37: Given a form with field foo, what should the validation method for this field be called?

A.   foo_clean

B.   validate_foo

C.   clean_foo

D.   foo_is_valid

38: You have created a Form class and wish to provide custom logic for validating the input in the "foo" field. What would you name your custom validation method?

A.   foo_clean

B.   clean_foo

C.   clean_foo_field

D.   sanitize_foo

E.   validate_foo

39: What is the Django command to retrieve the first 10 'User' records from your database sorted by name descending?

A.   User.objects.all().order('-name')[:10]

B.   User.all().order_by('-name')[10:]

C.   User.objects.all().order('-name')[10:]

D.   User.objects.all().order_by('-name')[:10]

E.   User.objects.all().order_by('name')[:10]

40: How to create a DateTimeField named created and filled in only on the creation with the current time?

A.   created = models.DateTimeField(auto_now=True)

B.   created = models.DateTimeField(

C.   created = models.CreationTimeField()

D.   created = models.DateTimeField(auto_now_add=True, auto_now=True)

E.   created = models.DateTimeField(auto_now_add=True)

41: You can handle multiple Django forms with what keyword argument when creating forms?

A.   suffix

B.   prefix

C.   name

D.   infix

42: When customizing validation in a Form subclass named MyForm, how do you add an error message that is displayed as a form-wide error?

A.   Raise ValidationError in MyForm.clean_<fieldname>()

B.   Raise ValidationError in MyForm.clean()

C.   Add the error to MyForm.errors in MyForm.clean()

D.   Add the error to MyForm._errors in MyForm.clean()

43: What command do you use to alter the fields Django displays for a given model in the Django admin ListView?

A.   fields_display

B.   list_filter

C.   fields_list

D.   auto_list_fields

E.   list_display

44: What is the command used to print the CREATE TABLE SQL statements for the given app name?

A. dumpdata myapp

B.   ./ schema myapp

C.   ./ sql myapp

D.   ./ showschema myapp

E.   ./ showsql myapp

45: In, when DEBUG is set to ________, Django will email unhandled exceptional conditions.

A.   True

B.   Always

C.   1

D.   Never

E.   False

46: You have a Form defined with "password" and "confirm_password" fields. In what method of the "form" object would you validate that the values supplied in these fields match?

A.   form.clean_password

B.   form.clean_confirm_password

C.   form.sanitize_data

D.   form.clean

E.   form.validate

47: How do you create a recursive relationship in a model class named 'Company' in Django?

A.   models.ForeignKey('self')

B.   models.ForeignKey('Company')

C.   models.ForeignKey('me')

D.   models.ForeignKey(Company)

48: Which class is a model field representing a path to a server-based image file?

A.   django.db.models.fields.files.ImageFieldFile

B.   django.db.models.fields.files.ImageFile

C.   django.db.models.fields.files.ImageFileField

D.   django.db.models.fields.files.ImageFileDescriptor

E.   django.db.models.fields.files.ImageField

49: Which of these can be used to retrieve a setting from the settings module, and provide a default if the setting is not defined?

A.   settings.get("SETTING_NAME", default_value)

B.   getattr("SETTING_NAME", settings, default=default_value)

C.   get_setting("SETTING_NAME", default=default_value)

D.   getattr(settings, "SETTING_NAME", default_value)

50: The Benevolent Dictators for Life of the Django Project are:

A.   Guido van Rossum and Linus Torvalds

B.   Ian Bicking and Jannis Leidel

C.   and Armando De La Veloper

D.   Eric S. Raymond and Larry Wall

E.   Jacob Kaplan-Moss and Adrian Holovaty