programming
beginner
10 sample questions
Django (python) MCQ Practice Test
Models, migrations, and database queries.
Generating admin interfaces for data management.
Q1. In a Django project, you've defined a model with a CharField named 'description' and want to ensure it's displayed as a <textarea> in your form. Which Django form field type should you use to achieve this?
-
A. CharField with widget=Textarea ✓
-
B. TextField
-
C. TextareaField
-
D. FormField
Explanation: In Django forms, the field is forms.CharField rendered as a <textarea> by setting widget=forms.Textarea. There is no forms.TextField (that is a model field).
Q2. Which Django framework component is responsible for generating URLs from views?
-
A. Django templates
-
B. Django forms
-
C. Django URL resolver ✓
-
D. Django middleware
Explanation: The Django URL resolver is a component of the Django framework that is responsible for mapping URLs to views. It uses a regular expression-based syntax to define URL patterns and map them to specific views.
Q3. In Django, which built-in model field is used to handle file uploads?
-
A. Django's built-in FileField ✓
-
B. Pillow library
-
C. django-storages library
-
D. django-filebrowser library
Explanation: Django's FileField (and its ImageField subclass) is the built-in model field for file uploads, handling the upload and storing a path to the file. Pillow is an image-processing library, django-storages provides alternative storage backends such as S3, and django-filebrowser is an admin file manager.
Q4. What is the primary purpose of the Django's built-in "django.views.static" module in serving files?
-
A. To handle HTTP requests for static files ✓
-
B. To serve dynamic content generated by views
-
C. To cache frequently accessed database queries
-
D. To implement CSRF protection in forms
Explanation: The "django.views.static" module is used to serve static files in Django applications. It provides a simple way to handle HTTP requests for static files, such as images, CSS files, and JavaScript files.
Q5. What is the purpose of the "django.dispatch.Signal" class in Django?
-
A. It is used to create a new view in Django
-
B. It is a built-in decorator for handling signals in Django
-
C. It is used to send a signal to all registered receivers in the application ✓
-
D. It is a built-in middleware for handling request and response in Django
Explanation: The "django.dispatch.Signal" class in Django is used to send a signal to all registered receivers in the application. This allows for loose coupling between different parts of the application, making it easier to modify or replace components without affecting other parts.
Q6. What is the purpose of the "django.conf" module in Django?
-
A. It provides a way to override default settings for a project ✓
-
B. It allows developers to define custom database models
-
C. It enables the use of custom authentication backends
-
D. It serves static files and media files
Explanation: The "django.conf" module in Django is used to override default settings for a project. This can be done by creating a "settings.py" file in the project directory, where developers can define custom settings and override default ones.
Q7. Which Django framework component is responsible for URL routing and is typically defined in the 'urls.py' file?
-
A. Template Engine
-
B. ORM (Object-Relational Mapping) system
-
C. View Resolver ✓
-
D. Cache Framework
Explanation: The View Resolver is a key component of the Django framework, responsible for mapping URLs to specific views. It is typically defined in the 'urls.py' file and is essential for routing URLs to the correct views in a Django application.
Q8. What is the purpose of the "django.contrib" module in a Django project?
-
A. To define custom database models
-
B. To enable authentication and authorization
-
C. To provide a set of reusable applications that can be used in a project ✓
-
D. To configure the project's URL dispatcher
Explanation: The "django.contrib" module contains a set of reusable applications that can be used in a Django project, including those for authentication, administration, and more. These applications can be enabled or disabled as needed in the project's "INSTALLED_APPS" setting.
Q9. Which built-in Django middleware emails the site administrators when a request 404s after following a broken link from another site?
-
A. django.middleware.common.BrokenLinkEmailsMiddleware ✓
-
B. django.middleware.common.CommonMiddleware
-
C. django.middleware.csrf.CsrfViewMiddleware
-
D. django.middleware.gzip.GZipMiddleware
Explanation: BrokenLinkEmailsMiddleware (working with CommonMiddleware; tunable via IGNORABLE_404_URLS) notifies ADMINS about 404 hits with external referrers. Custom 404 pages are handled by the handler404 view configured in urls.py, not by middleware - which is why CommonMiddleware was the wrong key.
Q10. In Django, which built-in middleware class is responsible for ensuring that the user is authenticated before accessing certain views?
-
A. django.middleware.common.CommonMiddleware
-
B. django.contrib.sessions.middleware.SessionMiddleware
-
C. django.contrib.auth.middleware.AuthenticationMiddleware ✓
-
D. django.middleware.csrf.CsrfViewMiddleware
Explanation: The AuthenticationMiddleware is a built-in middleware class in Django that ensures that the user is authenticated before accessing certain views. It checks for the presence of a valid user session and redirects the user to the login page if they are not authenticated.
That was just a sample. Sign up to unlock the full question bank with timed tests and certificates.
Sign Up Free