0% found this document useful (0 votes)
42 views7 pages

PYTHON Django

Uploaded by

tomarakash7800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

PYTHON Django

Uploaded by

tomarakash7800
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1. What is Django?

 Answer: Django is a high-level Python web framework that promotes rapid


development and clean, pragmatic design.
 Key Points:
o Follows the “Don’t Repeat Yourself” (DRY) principle.
o Includes built-in tools for ORM, authentication, and templating.

2. Explain Django's MTV architecture.

 Answer: MTV stands for Model-Template-View, a variant of the MVC pattern.


 Key Points:
o Model: Handles data and database interactions.
o Template: Renders HTML, with dynamic data from the model.
o View: Processes user input, interacts with the model, and returns a response.

3. How does Django handle database migrations?

 Answer: Using Django’s migrations framework, managed by the makemigrations


and migrate commands.
 Key Points:
o Automatic detection of changes in models.
o Migration files are created for schema changes.

4. What is a Django model?

 Answer: A Django model is a Python class that maps to a database table.


 Key Points:
o Defines fields and behaviors of the data.
o Supports various field types like CharField, IntegerField, etc.

5. How do you define relationships in Django models?

 Answer: Using ForeignKey, OneToOneField, and ManyToManyField.


 Key Points:
o ForeignKey: Many-to-one relationships.
o OneToOneField: One-to-one relationships.
o ManyToManyField: Many-to-many relationships.

6. What are Django templates?

 Answer: Django templates are a way to generate dynamic HTML content.


 Key Points:
o Use template language with tags and filters.
o Supports template inheritance.

7. Explain the use of Django admin interface.


 Answer: The Django admin interface provides a web-based tool for managing
models.
 Key Points:
o Automatic admin interface for models.
o Customizable using ModelAdmin.

8. What is Django ORM?

 Answer: Django ORM is an abstraction layer that allows interaction with the
database using Python code instead of SQL.
 Key Points:
o Allows database queries using Python objects.
o Supports various relational databases.

9. How does Django handle form validation?

 Answer: Using Django Forms and ModelForms, which include built-in validation.
 Key Points:
o Automatic validation for fields based on data types.
o Custom validation using clean() methods.

10. What is a Django view?

 Answer: A Django view is a Python function or class that receives a web request and
returns a web response.
 Key Points:
o Can be function-based (FBV) or class-based (CBV).
o Handles business logic and data presentation.

11. Explain Django’s middleware.

 Answer: Middleware is a way to process requests globally before they reach the view
or after the view processes them.
 Key Points:
o Can modify requests and responses.
o Used for tasks like authentication and logging.

12. What are Django signals?

 Answer: Signals allow certain senders to notify a set of receivers when an action has
taken place.
 Key Points:
o Used to decouple code.
o Common signals include pre_save, post_save.

13. How does Django handle static files?

 Answer: Django uses the STATICFILES_DIRS and STATIC_URL settings to serve static
files.
 Key Points:
o collectstatic command gathers static files in one directory.
o Configurable for development and production.

14. What is a Django context?

 Answer: Context in Django templates is a dictionary mapping variable names to


values.
 Key Points:
o Passed from views to templates.
o Allows dynamic content rendering.

15. How do you implement authentication in Django?

 Answer: Using Django’s built-in authentication framework.


 Key Points:
o Supports user login, logout, and registration.
o Middleware for session management.

16. What is a Django URL dispatcher?

 Answer: The URL dispatcher uses a set of patterns to route URLs to views.
 Key Points:
o Uses regex or path converters.
o Configured in urls.py.

17. How do you handle file uploads in Django?

 Answer: Using FileField or ImageField in models and handling them with


MEDIA_URL and MEDIA_ROOT.
 Key Points:
o File storage settings.
o Proper form handling in views.

18. What are Django generic views?

 Answer: Generic views provide pre-built views for common tasks like displaying
objects or forms.
 Key Points:
o Include ListView, DetailView, etc.
o Reduces repetitive code.

19. Explain Django’s caching mechanism.

 Answer: Django supports various caching backends like in-memory, file-based, and
database caching.
 Key Points:
o Configurable with CACHE settings.
o Supports view and template caching.
20. What are Django REST framework (DRF) serializers?

 Answer: DRF serializers convert complex data types, such as querysets, into JSON.
 Key Points:
o Support validation and transformation.
o Useful for APIs.

21. How does Django handle session management?

 Answer: Django uses a middleware to handle sessions, storing session data on the
server side.
 Key Points:
o Session data is saved in the database, cache, or file system.
o Configurable session backends.

22. What is Django's CSRF protection mechanism?

 Answer: Django uses a CSRF token to protect against cross-site request forgery
attacks.
 Key Points:
o Automatically included in forms with {% csrf_token %}.
o Middleware checks the token for validity.

23. How do you create and use custom management commands in Django?

 Answer: Custom management commands are created by adding a


management/commands directory to an app and defining a Python module for the
command.
 Key Points:
o Use BaseCommand to define the command.
o Accessible via python manage.py <command_name>.

24. What is a QuerySet in Django?

 Answer: A QuerySet is a collection of database queries that can be filtered, ordered,


and evaluated to retrieve data.
 Key Points:
o Lazy evaluation.
o Supports methods like filter(), exclude(), annotate(), etc.

25. Explain the use of get_object_or_404 and get_list_or_404.

 Answer: These are shortcuts used to retrieve an object or list of objects and raise a
404 error if not found.
 Key Points:
o Simplifies error handling in views.
o get_object_or_404: Retrieves a single object.
o get_list_or_404: Retrieves a list of objects.
26. What are Django signals, and when would you use them?

 Answer: Signals are a way to allow decoupled applications to get notified when
certain actions occur.
 Key Points:
o pre_save, post_save, pre_delete, etc.
o Used for logging, sending notifications, etc.

27. How do you optimize database queries in Django?

 Answer: By using select_related, prefetch_related, and only/defer to


minimize the number of database hits.
 Key Points:
o select_related: Joins related objects in the same query.
o prefetch_related: Executes separate queries for related objects.
o only/defer: Select specific fields.

28. Explain Django's transaction management.

 Answer: Django uses transaction management to ensure data integrity and rollback
changes if an error occurs.
 Key Points:
o transaction.atomic(): A block that enforces all-or-nothing transactions.
o Automatic transactions with ATOMIC_REQUESTS.

29. What is a Django context processor?

 Answer: Context processors add common variables to the context of all templates.
 Key Points:
o Configured in TEMPLATES settings.
o Useful for global data like user info or site settings.

30. How do you create custom template tags and filters?

 Answer: Create a templatetags directory in an app and define custom tags/filters


using the @register.simple_tag or @register.filter decorators.
 Key Points:
o Must be loaded in templates with {% load custom_tags %}.
o Used to extend template functionality.

31. Explain Django’s request and response objects.

 Answer: Django provides HttpRequest and HttpResponse classes to represent HTTP


requests and responses.
 Key Points:
o HttpRequest: Contains metadata about the request.
o HttpResponse: Used to return data to the client.

32. How do you set up email sending in Django?


 Answer: Configure email backend settings like EMAIL_HOST, EMAIL_PORT,
EMAIL_HOST_USER, etc., and use send_mail or EmailMessage.
 Key Points:
o Supports SMTP and third-party services.
o Asynchronous sending with celery or django-background-tasks.

33. What is the purpose of the urls.py file in a Django project?

 Answer: urls.py maps URLs to views in Django.


 Key Points:
o Defines URL patterns for the app.
o Uses path or re_path to define routes.

34. How do you handle 404 errors in Django?

 Answer: Create a custom 404 error page by defining a 404.html template and
configuring handler404 in urls.py.
 Key Points:
o Can log 404 errors.
o Custom 404 views for dynamic content.

35. Explain the use of Django's reverse function.

 Answer: The reverse function returns the URL path for a given view name and
optional parameters.
 Key Points:
o Used in views and templates for URL resolution.
o Avoids hardcoding URLs.

36. What is a Django mixin?

 Answer: A mixin is a reusable class-based view component that adds functionality to


a view.
 Key Points:
o Used for code reuse and separation of concerns.
o Common mixins: LoginRequiredMixin, PermissionRequiredMixin.

37. How do you use the Django Debug Toolbar?

 Answer: Install and configure the django-debug-toolbar package to monitor SQL


queries, cache usage, and other debug information.
 Key Points:
o Displays panels for debugging in the browser.
o Configurable and extensible.

38. Explain the @login_required decorator.

 Answer: The @login_required decorator restricts access to a view, allowing only


authenticated users to access it.
 Key Points:
o Redirects to login page if user is not authenticated.
o Customizable with login_url parameter.

39. What is Django’s get_absolute_url method?

 Answer: The get_absolute_url method in a model returns the URL for a particular
object instance.
 Key Points:
o Used in templates and views for object-specific URLs.
o Convention for object URL resolution.

40. How do you implement custom user models in Django?

 Answer: By creating a custom user model that extends AbstractBaseUser or


AbstractUser.
 Key Points:
o Custom user manager required.
o Defined in AUTH_USER_MODEL setting.

You might also like