DEV Community

Vincent Tommi
Vincent Tommi

Posted on

Django Interview Questions & Key Concepts – Part 2

1 What is difference between a project and an app?
a project serves as the overarching container for your web application, holding configurations and one or more apps that provide specific functionalities or features for your website .
anappis a reusable component within a Django project,designed to handle specific feature or functionality.

2 Explain Django’s architecture.

Django follows a Model-View-Template (MVT) architecture, which is a variation of the more common Model-View-Controller (MVC) pattern. The MVT pattern in Django helps separate business logic, user interface, and data handling to keep your code modular and maintainable.

🔧 1. Model

  • Represents the data layer of the application.
  • Defines the structure of your database using Python classes.
  • Automatically translates Python code into database tables via Django’s Object-Relational Mapper(ORM).

Example: You can define a User model with fields like name, email, etc., and Django will handle creating the appropriate SQL tables.

from django.db import models

class User(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
Enter fullscreen mode Exit fullscreen mode

🖼️ 2. View

  • Represents the business logic layer.
  • Handles HTTP requests and returns HTTP responses. -Retrieves data from the model and passes it to the template for rendering. In Django, views are written as Python functions or classes.
from django.shortcuts import render
from .models import User

def user_list(request):
    users = User.objects.all()
    return render(request, 'user_list.html', {'users': users})
Enter fullscreen mode Exit fullscreen mode

🎨 3. Template

  • Represents the presentation layer (user interface).
  • Templates are HTML files that use Django’s templating language to render data dynamically.
  • They separate design (HTML, CSS) from Python logic.
<!-- user_list.html -->
<h1>User List</h1>
<ul>
  {% for user in users %}
    <li>{{ user.name }} - {{ user.email }}</li>
  {% endfor %}
</ul>
Enter fullscreen mode Exit fullscreen mode

conclusion
🔄 How MVT Works Together:
User sends a request (e.g., /users/).

  • Django’s URL dispatcher maps the URL to the appropriate view.

  • The view queries the model for data.

  • The view passes that data to a template.

  • The template renders the data into HTML.

  • The response is returned to the browser.

3 What are the features of Django?
Batteries Included Philosophy: Django follows a "battery included", philosophy meaning provides a set of comprehensive features outside the box, such as object relational Mapping(ORM), Admin Panel,authentication,form handling,and more
which speed up development process.

Object relational mapping(ORM): Python Django provides a powerful orm that helps with interacting with database through python object and classes instead of writing raw SQL queries., making database operations more Pythonic and easier to manage.

MTV Architecture:Django's Model-Template-View (MTV) architecture is a well-structured and organized way to develop web applications. It separates the data (Model), the user interface (Template), and the business logic (View), making it easier to manage and maintain the code.

Admin Panel: Django provides an automatic admin interface based on the models, which allows developers to perform CRUD (Create, Read, Update, Delete) operations without writing additional code.

Security Features: Django has built-in protection against various web vulnerabilities, including SQL injection, CSRF (Cross-Site Request Forgery), and XSS (Cross-Site Scripting), making it a secure choice for web application development.

Admin Panel: Django provides an automatic admin interface based on the models, which allows developers to perform Create, Read, Update, Delete (CRUD) operations without writing additional code.

URL Routing: Django provides a simple and flexible URL routing mechanism that enables developers to design clean and SEO-friendly URLs.

Template Engine: Django comes with its own template engine, allowing developers to create dynamic HTML templates using template tags and filters.

Documentation and Community Support: Django has excellent and comprehensive documentation, making it easier for developers to learn and understand the framework. Additionally, it has a large and active community of developers, which means plenty of resources, tutorials, and third-party packages are available to help with development.

Form Handling: Django provides a powerful form handling system that simplifies the process of collecting and validating user input.

Authentication and Authorization: Django includes a robust authentication system that allows developers to manage user accounts, permissions, and groups easily.

4 Explaining the Django project directory Structure :
manage.py:it is automatically created in each Django project and located at the root directory of the project, it provides access to various administrative tasks and management commands to specific project such as starting the development server, running tests and creating migrations.

init.py: ensures the directory is treated as package and it's content to be imported correctly.

setting.py: It is the most important file in Django projects. It holds all the configuration values that your web app needs to work, i.e. pre-installed, apps, middle-ware, default database, API keys, and a bunch of other stuff.

views.py: is crucial as it contains the logic for handling web requests and generating appropriate responses. This file typically includes Python functions or classes that receive an HTTP request and return an HTTP response, which can be HTML content, a redirect, or an HTTP error like a 404.

urls.py: Crucial in mapping Urls to Views,allowing developers to handle request efficiently.

wsgi.py: This file in a Django project serves as an interface between the application server and the Django application. It is created automatically when you start a new Django project using django-admin startproject. This file contains a callable object that adheres to the Web Server Gateway Interface (WSGI) standard, allowing the server to communicate with your Django application.

models.py: The Model represents the models of web applications in the form of classes, it contains no logic that describes how to present the data to a user.

admin.py:This file in a Django app is used to display your models in the Django admin panel and allows for customization of the admin interface. This file is crucial for registering models with the admin site, enabling CRUD operations for those models through a web-based interface.

apps.py:This file in a Django app is used to define the AppConfig class, which stores metadata for the application and allows for customization of certain attributes of the application.

Top comments (0)