This defines how the frameworks handle and respond to requests, and also its middleware system
Explain the project directory structure.
Django has 5 essential files. Every project has the settings.py, manage.py, urls.py, wsgi.py and __init__.py files.
- settings.py - Settings or configurations for the project. This is where you can set up your database, and cache connections, password validators, timezones etc.
- manage.py - Used for administrative tasks. This works like the django-admin utility, you can run migrations, collect static, clear sessions, and start/stop projects.
- urls.py - Have all URLs for the project.
- wsgi.py - This is the entry point for the application and is used by web servers to serve the project.
- __init__.py - Declaring the project directory as a python package.
Then each Django application will also have its own directory structure. It consists of urls.py, models.py, views.py, admin.py, test.py and apps.py.
Explain the Django Architecture
Django uses the MVT (Model View Template) architecture. The Model handles the database, in other frameworks, you would have a Controller to manage the querying of the database but in Django, the system handles the controlling stuff.
- Model - Defining database using ORM (Object Relational Mapper)
- View - Manages the logic of receiving a web request and returning a web response.
- Template - This is the presentation layer that uses Django Template Language (DTL).
What is the difference between a Django project and a Django app?
App in Django is web applications with a specific task, for example, a surveys app, or a blog app. And the Django project is a collection of those apps. Any Django project has one more app put together to form a web system.
What are static files in Django?
Explain Django Views
Django Views are responsible for processing user requests. The views return an HttpResponse or Exception like the famous HTTP404. Each URL in a Django project is connected to a view which is responsible for response logic.
from django.http import HttpResponse def hello_world(request): return HttpResponse(“Hello World")
What are Django middlewares?
Middlewares are functions that execute during the request and response process. Middlewares have many functions such as CSRF protection, authentication and security.
What are templates?
The Django template engine is responsible for presenting the information to the end user. Templates are used to dynamically generate the final HTML that'll be rendered on the client's browser
General Django interview questions and answers
How to reuse code in a Django project?
Django is different from other web frameworks. A typical Django project is divided into several applications. Once you develop an app, you can reuse it in several other Django project projects by just copying and pasting it.
What is the latest version of Django?
Well, this is a bit tricky. As per this writing, the latest version is 4.0. But it keeps updating so you'll need to check the official Django website for updates and release notes.
Why do you use Django instead of other frameworks?
- It's a rapid framework, you can build and reuse your apps.
- Secure and scalable.
- Being a Python-based framework, all advantages of python such as readability, ease to learn, portability etc are also provided.
- Also if you're experienced in building with Django, it becomes more and more convenient to use it because of experience and reusability.
- All python libraries can work in Django.
- Huge active community.
These are the reasons why you may choose Django over other frameworks like PHPs Laravel.
Is Django a content management system (CMS)?
What is Jinja Templating in Djnago?
Jinja is a templating engine for python. Features include:
- HTML escaping
- Template inheritance
- Generates HTML faster
What are different inheritance styles in Django?
- Abstract base class - This is when you have a parent class that will hold some data that you don't want to repeat in individual child classes. For example, you can use AbstractUser class to create user models, say the Teacher model and Student Model.
- Multi-table inheritance - Used when you subclass an existing table and all subclasses will have their own table.
- Proxy models - Used when you only want to modify the python level behaviour of a table, without altering the model fields.
What is a mixin in Django?
Mixin classes employ multiple inheritances and so you can combine functions and attributes of multiple parent classes. It allows developers to reuse code from multiple classes.
Why a permanent redirects a bad idea?
Responses of permanent redirects are cached by the browser, and there'll be issues when you try to redirect to somewhere else.
Here is a list of database questions and answers for Django interviews
What is an Object Relational Mapper (ORM)?
It's a library that automates the transfer of data in databases into objects used in the application and vice-versa. ORMs allow a developer to write python code and not the ever hard, and confusing SQL and therefore speeding up the development.
What are Django signals?
Django signals help to execute tasks based on events. For example, you want to send an email whenever a new user has registered. Here is a list of built-in signals:
Django built-in signals in models
- django.db.models.signals.pre_save & django.db.models.signals.post_save - these are called before or after a models save() method was called.
- django.db.models.pre_init & django.db.models.post_init - Triggered before or after a models __init__() method is called.
- django.db.models.signals.pre_delete & django.db.models.signals.post_delete - Triggered before and after a models delet() method is called.
- django.db.models.signals.m2m_changed - Triggered when a many to many field is changed.
- django.core.signals.request_started & django.core.signals.request_finished - Triggered when an HTTP request has started and finished.
Explain the Django Field class
Django's Field class is an abstract class which represents a column in a database table. The Field classes are used to create columns in a database table.
What is n+1 problem in Django?
It is a data access problem where a query is run for every result of the previous query. This problem can be fixed by using the select_related query which creates a SQL join and includes all fields of the related object in the SELECT statement.
Explain caching in Django
Caching is saving the result of an expensive query so that you don't have to repeat it. Django comes with a ready-to-use caching system which you can employ the make your website fast while consuming fewer resources. Here is the list of Django caching strategies.
- Memcached - A memory-based cache server like Redis.
- Database Caching - The cache is stored in the database. This will work very well if your database is fast and well indexed.
- Filesystem caching - The cache is stored in separate files in a serialized order.
- Local memory caching - Default cache for Django if you didn't specify any. The cache is per process and thread-safe.
If you're working with long JSON data in your APIs then it's also advised to use caching.