---> Django Interview <<--
1. Django :
-- Django is a web framework of python.
-- Framework : in framework, we have to follow its pre-defined folder structure and
rules. eg: nextjs, Django, angular
--> Django works on MVT (Model–View–Template) Pattern.
-- MVT is a software design pattern
Model → it represents database login like creating table, getting data from table
View → Handles logic/functions, whatever you see on browser when you render
website, that's due to view
Template → Handles HTML pages.
--> Why Django ?
Django is used because it makes web development faster, more secure, and scalable.
It comes with built-in features like authentication, admin panel, ORM, and security
protections.
2. Project Setup commands :
-- install virtual env : pip install virtualenv
-- create virtual env : py -m virtualenv env_name
-- activate virtual env : env_name/Scripts/activate
-- install Django : pip install Django
-- create project : django-admin startproject project_name
-- create app : python manage.py startapp app_name
-- run/start server : python manage.py runserver
-- python manage.py makemigrations : create model
-- python manage.py migrate : convert model into table
-- virtual env : contains all installed dependencies which we use in Django Project
(its like package.json in nodejs)
3. Project directory folder details :
1. manage.py : it is the main file which is used to run project, migrate project
2. create 'template' name folder inside project directory : where we add our html
pages
3. create 'static' name folder inside project directory : where javascript, css,
fonts files are present
4. create 'media' name folder inside project directory : it contains dynamic images
and files
--these above three folders we have to make by ourself
5. db.sqlite : default database
6. settings.py : it manages all files present in project, which stores important
settings like database connection, installed apps, middlewares. because manage.py
internally hits settings.py
7. urls.py : it works as a routing by providing path to it, which tells at what
path, what page will be open.
8. forms.py : its is used to handle user input(text boxes) and save data into
model.
9. views : views are python function that take http request and return http
response like html document, texts.
4. Template tags :
1. include : is used to insert reusable html code into another html file. i.e.,
{% include "file_name" %}
2. extends : it is like inheritance, means one template can reuse the structure of
another template. ie., {% extends "file_name" %}
5. Error handling : it can be done using 'try - except' block
404 not found -> URL not mapped
500 Server error -> view code crashed
6. ORM(object Relational Mapping) : it is a technique that allows you to interact
with a database using Python Object instead of SQL queries.
7. Migration :
-- Migrations are a way to keep track of changes in your database structure
(schema) when your models change. Its like git.
-- migrate : It is used to create default table (admin, auth, session) in
db.sqlite3 file. It is used to apply changes to database or update the database.
-- Command : python manage.py migrate
8. Superuser :
-- It give access to enter in admin account with full permission to manage entire
project via Django admin site.
9. Template : contains HTML file with dynamic data (python data + html design)
10. DTL (Django Template language) : allows to write python like code inside html.
It uses :
1. {{ }} : for variables
2. {% %} : for tags (loops, if-else, logic, etc)
11. HTTP Request : (hyper text tranfer protocol) :
-- HTTP Request is a message sent by client(such as browser) to a server,
requesting for specific resource
--> Http methods :
1. Get : used to fetch data from server. Data is sent in URL query string, starting
with '?'
2. Post : used to send data to the server (submitting form). Data is sent in the
request body.
3. Put : update the data
4. Delete : remove the data
12. CSRF :
-- CSRF (cross-site request forgery) token : it protects the form from hackers by
verifying the POST requests really come from your own website.
-- When we load page, django generate random unique hidden token
-- CSRF is used with POST method
-- If CSRF is missing then it gives error : forbidden (403)
-- use : {% csrf_token %}
13. How do we redirect to new page : using 'HttpResponseRedirect'
14. Foreign Key : A ForeignKey creates a relationship between two models.
15. What is Django Admin?
It’s a built-in interface to manage database records (CRUD operations). It allows
creating, updating, deleting, and searching data via a web interface.
16. Cookies → stored in the client’s browser (less secure).
Sessions → stored on the server