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?

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?

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?

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?

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?

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?

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?

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?

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?

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?

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
Django (python) MCQ Practice Test | Mind Hustle