Django Beginner Flashcards

1
Q

Explain Django Architecture

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Explain the django project directory structure

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are models in Django

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are templates in Django or Django template language

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are views in Django

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Django ORM

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Define static files and explain their uses

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Django Rest Framework(DRF)?

A

Django Rest Framework is an open-source framework based upon Django which lets you create RESTful APIs rapidly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is django-admin and manage.py and explain its commands

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is Jinja templating

A

Jinja Templating is a very popular templating engine for Python, the latest version is Jinja2.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are Django URLs

A
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)
]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between a project and an app in Django

A
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).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are different model inheritance styles in the Django

A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly