Django Beginner Flashcards
Explain Django Architecture
Django follows the MVT (Model View Template) pattern which is based on the Model View Controller architecture. It’s slightly different from the MVC pattern as it maintains its own conventions, so, the controller is handled by the framework itself. The template is a presentation layer. It is an HTML file mixed with Django Template Language (DTL). The developer provides the model, the view, and the template then maps it to a URL, and finally, Django serves it to the user
Explain the django project directory structure
manage.py - A command-line utility that allows you to interact with your Django project
__init__.py - An empty file that tells Python that the current directory should be considered as a Python package
settings.py - Comprises the configurations of the current project like DB connections.
urls.py - All the URLs of the project are present here
wsgi.py - This is an entry point for your application which is used by the web servers to serve the project you have created.
What are models in Django
A model in Django refers to a class that maps to a database table or database collection. Each attribute of the Django model class represents a database field. They are defined in app/models.py
Example:
from django.db import models class SampleModel(models.Model): field1 = models.CharField(max_length = 50) field2 = models.IntegerField() class Meta: db_table = “sample_model”
Every model inherits from django.db.models.Model
Our example has 2 attributes (1 char and 1 integer field), those will be in the table fields.
The metaclass helps you set things like available permissions, singular and plural versions of the name, associated database table name, whether the model is abstract or not, etc.
What are templates in Django or Django template language
Templates are an integral part of the Django MVT architecture. They generally comprise HTML, CSS, and js in which dynamic variables and information are embedded with the help of views. Some constructs are recognized and interpreted by the template engine. The main ones are variables and tags.
A template is rendered with a context. Rendering just replaces variables with their values, present in the context, and processes tags. Everything else remains as it is.
The syntax of the Django template language includes the following four constructs :
Variables Tags Filters Comments
What are views in Django
A view function, or “view” for short, is simply a Python function that takes a web request and returns a web response. This response can be HTML contents of a web page, or a redirect, or a 404 error, or an XML document, or an image, etc.
Example:
from django.http import HttpResponse def sample_function(request): return HttpResponse(“Welcome to Django”)
There are two types of views:
Function-Based Views: In this, we import our view as a function. Class-based Views: It’s an object-oriented approach.
What is Django ORM
This ORM (an acronym for Object Relational Mapper) enables us to interact with databases in a more pythonic way like we can avoid writing raw queries, it is possible to retrieve, save, delete and perform other operations over the database without ever writing any SQL query. It works as an abstraction layer between the models and the database
Define static files and explain their uses
Websites generally need to serve additional files such as images. Javascript or CSS. In Django, these files are referred to as “static files”, Apart from that Django provides django.contrib.staticfiles to manage these static files
What is Django Rest Framework(DRF)?
Django Rest Framework is an open-source framework based upon Django which lets you create RESTful APIs rapidly
What is django-admin and manage.py and explain its commands
django-admin is Django’s command-line utility for administrative tasks. In addition to this, a manage.py file is also automatically created in each Django project. Not only does it perform the same purpose as the django-admin but it also sets the DJANGO_SETTINGS_MODULE environment variable to point to the project’s settings.py file.
What is Jinja templating
Jinja Templating is a very popular templating engine for Python, the latest version is Jinja2.
What are Django URLs
URLs are one of the most important parts of a web application and Django provides you with an elegant way to design your own custom URLs with help of its module known as URLconf (URL Configuration). The basic functionality of this python module is to You can design your own URLs in Django in the way you like and then map them to the python function (View function). These URLs can be static as well as dynamic. These URLs as present in the urls.py where they are matched with the equivalent view function.
Basic Syntax:
from django.urls import path from . import views urlpatterns = [ path('data/2020/', views.data_2020), path('data//', views.data_year) ]
What is the difference between a project and an app in Django
In simple words Project is the entire Django application and an app is a module inside the project that deals with one specific use case. For eg, payment system(app) in the eCommerce app(Project).
What are different model inheritance styles in the Django
Abstract Base Class Inheritance: Used when you only need the parent class to hold information that you don’t want to write for each child model. Multi-Table Model Inheritance: Used when you are subclassing an existing model and need each model to have its own table in the database. Proxy Model Inheritance: Used when you want to retain the model's field while altering the python level functioning of the model.