1.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.
2. 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.
3. 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.