0% found this document useful (0 votes)
27 views46 pages

Django Notes

The document provides a comprehensive guide on setting up a Django project, including creating a virtual environment, installing necessary packages, and configuring project structure. It covers essential components such as the manage.py file, migrations, Django admin panel, and app creation, along with detailed explanations of models, views, and templates. Additionally, it discusses user authentication, profile management, and the use of signals for automatic profile creation upon user registration.

Uploaded by

Tejendra hsjajj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views46 pages

Django Notes

The document provides a comprehensive guide on setting up a Django project, including creating a virtual environment, installing necessary packages, and configuring project structure. It covers essential components such as the manage.py file, migrations, Django admin panel, and app creation, along with detailed explanations of models, views, and templates. Additionally, it discusses user authentication, profile management, and the use of signals for automatic profile creation upon user registration.

Uploaded by

Tejendra hsjajj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

🌐

Django

#Setting up project
(i) What is Virtual Environment in python?
For example, if you have two projects—one based on Machine Learning and
another on Django—each project will require different dependencies and
modules. Instead of installing all these dependencies globally, you can
create a separate virtual environment for each project. This allows you to
install only the required dependencies for each project within its own
environment, promoting separation of concerns and avoiding conflicts
between packages.

python -m pip install virtualenv #installing virtual environment package


python -m virtualenv venv #Creating virtual environment inside project fo
source venv/bin/activate #Activating virtaul environment

Django 1
(ii) Setting up vs code!
Download Django extension in vscode

Python intellesence extension

materialicons extension

(iii) Creating Django Project (go inside project folder


and activate venv)
pip install Django

django-admin help

django-admin startproject project_name and then change the main top level
folder project_name to src

cd src

python manage.py runserver

(iv) Manage.py File


Utility file allows us to interact with django project.

→ python manage.py --help

Migration:- django has a built in orm. it is basically a system which


abstracts the complexity of interacting with database. The orm allows us to
write python code to interact with sql database. When certain changes
happens to the python code they need to be translated into sql. The code
that becomes sql code reffered to as migration and we need to instruct
django that migrations has to apply to database. So migration in pythonn ar
code that we write to interact with database.

python manage.py migrate

(v) Project Structure (inside src folder)


db.sqlite3 → sql database used by our django project

Django 2
folder named your project. (small app within our django app)

Django project is a composition of smaller sub apps that comes together to


form a complete application. for example we have small part in our
application for authentication and authorization which is attached to the
main app.

asgi.py file asynchronus gateway interface allows external applications or


webservers to interact with your application.

wsgi.py file helps to deploy our application to production it servers as an


entry point for the applications that acts like web servers.

settings.py file contains all the settings for our django app for example
allowed host defines on which application our django application must be
served. Installed app section inisde which we defines what all are
applications that ccomes together to create our django app. Middlewares
section defines all the middlewares that comes between request and
response. Templating that allows you to embed dynamic data into your
static html pages.

urls.py → defines all of the urls or paths on the application that are
accessible.

(vi) Django Admin Pannel


The Django admin panel is a built-in web interface in Django that lets you
manage your app's data easily—like adding, editing, and deleting records—
without writing any code.

Pre-Built Backend Given by django.(login , admin )

django.contrib.auth

creating superuser having admin previedges

python manage.py createsuperuser

python manage.py changepassword

Django 3
#Basics
python manage.py startapp main

All Apps you created must be linked inside the installed app section of
settings.py file inside your main app.

(i)Django App
→ Migrations in Django are a way to save and apply changes you make to
your database tables.

For example, if you add a new field to a model (like a new column in a table), a
migration will record that change and update the database for you.

So, migrations help keep your Python models and your database structure in
sync.

📁 admin.py

Used to customize Django’s admin panel.

You register your models here so they show up in the admin dashboard.

from .models import MyModel


admin.site.register(MyModel)

📁 apps.py

Contains settings specific to your app, like the app's name.


Django uses it to recognize your app.

Usually, you don’t change it much.

📁 models.py

This is where you define your database tables using Python classes.

Each class = one table in the database.

Django 4
Example:

class Book(models.Model):
title = models.CharField(max_length=100)

📁 views.py

This is where you write what should happen when a user visits a page.
It connects the URL to the logic or the HTML page.

Example:

def home(request):
return render(request, 'home.html')

📁 tests.py

Used to write tests to make sure your app works correctly.

It’s like checking that your code is doing what it's supposed to.

Example:

def test_homepage(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200)

(ii) Django Views


Views in Django decide what to show when someone visits a webpage.

They take a request (like someone clicking a link) and return a response (like an
HTML page, some data, or a message).

Consider Following Folder Structure

Django 5
//views.py (inside main app)

from django.shortcuts import render


from django.http import HttpResponse

# Create your views here.

Django 6
def main_view(request):
return HttpResponse("<h1>Welcome to CarVibe!</h1>")

//urls.py (inside main app)

from django.urls import path


from .views import main_view

urlpatterns = [
path('',main_view , name='main')
]

//urls.py (inside carvibe app)

from django.contrib import admin


from django.urls import path,include

from main import urls as main_app_urls

urlpatterns = [
path("admin/", admin.site.urls),
path('', include(main_app_urls))

(iii) Django Templating


Templating in Django means:

Using HTML files with placeholders to show dynamic content


like names, lists, or data from the database.

Django 7
//main.html (inside views folder inside templates folder)

<h1>Welcome to {{name}}! </h1>

//views.py (inside views folder inside templates folder)

from django.shortcuts import render


from django.http import HttpResponse

Django 8
# Create your views here.

def main_view(request):
return render(request,"views/main.html" ,{"name:" "Carvibe"})

(iii) Loading static data


✅ 1. Set Up STATIC_URL in Settings
In your settings.py file:

STATIC_URL = '/static/'

# For production use


STATICFILES_DIRS = [BASE_DIR / "static"]

# For collectstatic in production


STATIC_ROOT = BASE_DIR / "staticfiles"

✅ 2. Create a static Directory


Inside your app or project root, create a directory called static .

Example structure:

myproject/
├── myapp/
│ └── static/
│ └── myapp/
│ └── styles.css
└── static/
└── global.css

#****************************************************************#

✅ 3. Use {% load static %} in Templates

Django 9
At the top of your template ( .html ) file, add:

{% load static %}

<link rel="stylesheet" href="{% static 'myapp/styles.css' %}">


<img src="{% static 'images/logo.png' %}" alt="Logo">

🧱 4. {% extends 'base.html' %}
This tells Django that your current template inherits from another template
(usually a base layout).

It must go at the very top of the template file.

🧩 5. {% block %}
This defines sections of content that can be filled in or overridden in child
templates.

base.html (the parent template)

{% load static %}
<!DOCTYPE html>
<html lang="en" class="h-100">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="Hussain Mustafa, Preneure">

{% block title %}
<title>AutoMax . List Your Car Today!</title>
{% endblock %}

<!-- Bootstrap core CSS -->


<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">

Django 10
<!-- Custom Style CSS -->
<link href="{% static 'css/style.css' %}" rel="stylesheet">

<!-- JQuery CDN -->


<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=
</head>

<!-- ✅ Add this -->


<body class="d-flex flex-column h-100">

{% if messages %}
{% for message in messages %}
<div {% if message.tags %} class="alert alert-{{ message.tags }}" {% en
{{ message }}
</div>
{% endfor %}
{% endif %}

{% block header %}
<header class="p-3 bg-dark text-white w-100 nav-masthead">
<div class="container">
<div class="d-flex flex-wrap align-items-center justify-content-center
<a href="" class="d-flex align-items-center mb-2 mb-lg-0 text-white
<img class="bi me-2" width="32" height="32" src="{% static 'ima
</a>
{% if request.user.is_authenticated %}
<ul class="nav col-12 col-lg-auto me-lg-auto mb-2 justify-content-c
<li>
<a href="" class="nav-link px-2">Profile</a>
</li>
</ul>
<div class="text-end">
<a type="button" href="" class="btn btn-danger">Logout</a>
</div>
{% endif %}
</div>
</div>

Django 11
</header>
{% endblock %}

<!-- ✅ Wrap body block in a main tag with flex-grow -->


<main class="flex-grow-1">
{% block body %}
{% endblock %}
</main>

{% block footer %}
<footer class="footer mt-auto text-muted bg-dark py-5">
<div class="container">
<p class="float-end mb-1">Powered by <a href="https://www.djangop
<p class="mb-1">AutoMax - Best Place To Buy & Sell Cars</p>
<p class="mb-0">© Copyright {% now 'Y' %} <a href="">AutoMax</a
</div>
</footer>
{% endblock %}

</body>
</html>

home.html (the child template)

{% extends 'base.html' %}

{% block title %}Home Page{% endblock %}

{% block content %}
<h1>Welcome to the Home Page!</h1>
<p>This is some content for the home page.</p>
{% endblock %}

Django 12
#Authentication
(i) Creating a users app and registering it inside
settings.py file inside mainapp
python manage.py startapp users

(ii) Creating users

Go to admin pannel and click on add users button.

creating a profile model to extend features or add more details about users
in admin pannel.

//models.py inside users folder

from django.db import models


from django.contrib.auth.models import User

# Create your models here.

# //every user must have only one profile/


class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE) #one us
photo = models.ImageField(null=True) #Not necessary field
bio = models.CharField(max_length=140,blank=True)
phone_number = models.CharField(max_length=12, blank=True)

def __str__(self):
return f'{self.user.username}\'s Profile'

python manage.py createmigrations

python manage,py migrate

Django 13
(iii) Registering the model to Django admin so that it
is accessible from admin pannel

//admin.py inside users folder

from django.contrib import admin


from .models import Profile
# Register your models here

#whatever functionality model admin allows profile admin will also allow those
class ProfileAdmin(admin.ModelAdmin):
pass

admin.site.register(Profile,ProfileAdmin)

(iv) Creating functionality so that profile gets


automatically created when user gets created
(Django Signals)
Signals in Django are a way for different parts of your app to talk to each other
automatically when something happens.

Django 14
For example:

When a new user is created, you might want to automatically create a


profile for them.

Instead of adding this code manually every time, you can use a signal that
says:
👉 "Hey! A new user was just saved — now go create their profile."
//signals.py inside users folder

# Import the Profile model from the current app's models.py


from .models import Profile

# Import the 'post_save' signal — this is triggered right after a model is sav
from django.db.models.signals import post_save

# Import the 'receiver' decorator — used to connect a function to a signal


from django.dispatch import receiver

# Import the built-in User model — we want to listen for when a new user i
from django.contrib.auth.models import User

# This is the function that will run **after a User is saved**


# The @receiver line connects the function to the post_save signal
# It says: "Run this function whenever a User is saved"
@receiver(post_save, sender=User) # "sender=User" means we're watch
def create_user_profile(sender, instance, created, **kwargs):
# sender = the model class that sent the signal (User)
# instance = the actual object being saved (a specific User)
# created = True if this is a **new** user, not just an update
# **kwargs = extra data we’re not using here, but it must be included

# Only run the code below if a new user was just created
if created:
# Automatically create a Profile for the new user
# Connects the Profile to this user using a one-to-one relationship
Profile.objects.create(user=instance)

Django 15
//__init__.py

# users/apps.py
from django.apps import AppConfig

class UsersConfig(AppConfig):
name = 'users'

def ready(self):
import users.signals # <-- This is where signals are usually connected

📍 Where it's used:


This line is placed in the __init__.py file of a Django app (in your case, the app is
named users ).

✅ What it does:
It tells Django which AppConfig class to use when setting up the app.

🔍 Breaking it down:
'users.apps.UsersConfig' is the path to your custom AppConfig class.

UsersConfig is a class in users/apps.py that looks like this:

# users/apps.py
from django.apps import AppConfig

class UsersConfig(AppConfig):
name = 'users'

def ready(self):
import users.signals # <-- This is where signals are usually connected

🧠 Why it's useful:


Django 16
It lets you run custom startup code when Django starts the app.

Most often, it's used to import and register signals (like the one you wrote).

(v) Creating Location Model

/models.py inside users folder

from django.db import models


from django.contrib.auth.models import User
from localflavor.us.models import USStateField ,USZipCodeField

# Create your models here.


class Location(models.Model):
address_1 = models.CharField(max_length=128 , blank=True)
address_2 = models.CharField(max_length=128 , blank=True)
city = models.CharField(max_length=64)
state = USStateField(default="NY")
zip_code = USZipCodeField(blank=True)

def __str__(self):
return f'Location {self.id}'

# //every user must have only one profile/


class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE) #one us
photo = models.ImageField(null=True) #Not necessary field
bio = models.CharField(max_length=140,blank=True)
phone_number = models.CharField(max_length=12, blank=True)
location = models.OneToOneField(Location, on_delete=models.CASCADE , n

Django 17
def __str__(self):
return f'{self.user.username}\'s Profile'

/admin.py inside users folder

from django.contrib import admin


from .models import Profile ,Location
# Register your models here

#whatever functionality model admin allows profile admin will also allow those
class ProfileAdmin(admin.ModelAdmin):
pass

class LocationAdmin(admin.ModelAdmin):
pass

admin.site.register(Profile,ProfileAdmin)
admin.site.register(Location,LocationAdmin)

/signals.py inside users folder

from .models import Profile


from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User

#When user objects gets saved in the database run the below function
#each user inside the database is a instance
@receiver(post_save,sender=User)
def create_user_profile(sender,instance,created ,**kwargs):
if created:
Profile.objects.create(user=instance)

Django 18
(VI) Automating Location using signals
(lcoation automatically gets created whever user is created)

//signals.py
from .models import Profile , Location
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User

#When user objects gets saved in the database run the below function
#each user inside the database is a instance
@receiver(post_save,sender=User)
def create_user_profile(sender,instance,created ,**kwargs):
if created:
Profile.objects.create(user=instance)

@receiver(post_save,sender=Profile)
def create_profile_location(sender,instance,created ,**kwargs):
if created:
profile_location = Location.objects.create()
instance.location = profile_location
instance.save()

(VII)Working with Media Files

→inside settings.py inisde main app folder

#Media Files Section(Uploaded files)


MEDIA_ROOT = os.path.join(BASE_DIR,'media')

Django 19
MEDIA_URL ='/media/'

→Create utils.py file inside users app folder

def user_directory_path(instance,filename):
return 'user_{0}/{1}'.format(instance.user.id , filename)

//urls.py inside main app folder

"""
URL configuration for carvibe project.

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/5.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.contrib import admin


from django.conf import settings
from django.urls import path,include

from main import urls as main_app_urls


from django.conf.urls.static import static

urlpatterns = [
path("admin/", admin.site.urls),
path('', include(main_app_urls))

Django 20
]

if settings.DEBUG:
urlpatterns+= static(settings.MEDIA_URL,document_root=settings.MEDIA_RO

#to make photos accessible from admin pannel

→EDit models.py files

from django.db import models


from django.contrib.auth.models import User
from localflavor.us.models import USStateField ,USZipCodeField
from .utils import user_directory_path

# Create your models here.


class Location(models.Model):
address_1 = models.CharField(max_length=128 , blank=True)
address_2 = models.CharField(max_length=128 , blank=True)
city = models.CharField(max_length=64)
state = USStateField(default="NY")
zip_code = USZipCodeField(blank=True)

def __str__(self):
return f'Location {self.id}'

# //every user must have only one profile/


class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE) #one us
photo = models.ImageField(upload_to=user_directory_path,null=True) #Not

Django 21
bio = models.CharField(max_length=140,blank=True)
phone_number = models.CharField(max_length=12, blank=True)
location = models.OneToOneField(Location, on_delete=models.SET_NULL ,

def __str__(self):
return f'{self.user.username}\'s Profile'

(VIII)Creating Login View

//views.py inside users folder

from django.shortcuts import render


from django.http import HttpResponse

# Create your views here.

def login_view(request):
return render(request , 'views/login.html')

//urls.py inside main app folder

from users import urls as users_app_urls


urlpatterns = [
path("admin/", admin.site.urls),
path('', include(main_app_urls)),
path('',include(users_app_urls))
]

//inside users app folder create templates folder and inside it create views
folder and inside it create login.html

Django 22
{% extends 'base/base.html' %}

{% block body %}
<h1>Login View</h1>

{% endblock %}

{% block footer %}
<h1>Footer</h1>
{% endblock %}

//urls.py inside users app folder

from django.urls import path


from .views import login_view

urlpatterns=[
path('login/',login_view , name='login')
]

(ix) Working with Forms

→Part 1
//views.py inside users app

from django.shortcuts import render


from django.http import HttpResponse
from django.contrib.auth.forms import AuthenticationForm
# Create your views here.

def login_view(request):

Django 23
login_form = AuthenticationForm()
return render(request , 'views/login.html',{'login_form': login_form})

//login.html inside users app

{% extends 'base/base.html' %}
{% load static %}
{% load crispy_forms_tags%}

{% block body %}
<div class="text-center">
<main class="form-signin">
<form action="" method="post">
<img class="mb-4" src="{% static 'images/logo.png' %}" alt="" width
{{ login_form | crispy}}
<button class="w-100 btn btn-lg btn-danger" type="submit">Sign In</
<p class="mt-5 mb-3 text-muted">Copyright © 2022 AutoMax</p>
</form>
</main>
</div>
{% endblock %}

{% block footer %}
{% endblock %}

//settings.py inside main app (carVibe)

INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"localflavor",
'crispy_forms',
'crispy_bootstrap5',

Django 24
"main",
"users",
]

#Django Crispy Form Settings


CRISPY_TEMPLATE_PACK = 'bootstrap5'
CRISPY_ALLOWED_TEMPLATE_PACKS = ['bootstrap5']

→ Part 2
//views.py inside users app

from django.shortcuts import render


from django.http import HttpResponse
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate
# Create your views here.

def login_view(request):
if request.method == 'POST':
login_form = AuthenticationForm(request=request, data=request.POST)
if login_form.is_valid():
username = login_form.cleaned_data.get('username')
password = login_form.cleaned_data.get('password')
user = authenticate(username=username,password=password)
if user is not None:
pass
else:
pass
elif request.method=='GET':
login_form = AuthenticationForm()
return render(request , 'views/login.html',{'login_form': login_form})

//login.html inside users app

Django 25
{% extends 'base/base.html' %}
{% load static %}
{% load crispy_forms_tags%}

{% block body %}
<div class="text-center">
<main class="form-signin">
<form action="" method="post">
<img class="mb-4" src="{% static 'images/logo.png' %}" alt="" width
{% csrf_token %}
{{ login_form | crispy}}
<button class="w-100 btn btn-lg btn-danger" type="submit">Sign In</
<p class="mt-5 mb-3 text-muted">Copyright © 2022 AutoMax</p>
</form>
</main>
</div>
{% endblock %}

{% block footer %}
{% endblock %}

#Important Info
→ 🔍 Django Filters (in templates):
Filters are used to modify or format data in Django templates.

Example:

{{ name|upper }}

JOHN

Django 26
✅ Common filters:
upper → makes text uppercase

lower → makes text lowercase

date → formats a date

length → counts the number of items

→ 🔒 CSRF Token:
CSRF stands for Cross-Site Request Forgery. It’s a security feature.
Django uses a CSRF token to protect forms from being submitted by hackers
or fake sites.
When you make a form that sends data (like login or signup), you must include
this:

<form method="POST">
{% csrf_token %}
<!-- form fields -->
</form>

#Logging User in
//src/main/views.py

from django.shortcuts import render


from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
# Create your views here.

def main_view(request):
return render(request,"views/main.html",{"name":"CarVibe"})

Django 27
#ensuring that home view is accessible only whn user is logged in
@login_required
def home_view(request):
return render(request , "views/home.html")

//src/main/urls.py

from django.urls import path


from .views import main_view , home_view

urlpatterns = [
path('', main_view, name='main'),
path('home/',home_view,name='home')
]

//src/users/views.py

from django.shortcuts import render , redirect


from django.http import HttpResponse
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate , login

# Create your views here.

def login_view(request):
if request.method== 'POST':
login_form = AuthenticationForm(request=request , data=request.POST)
if login_form.is_valid():
username = login_form.cleaned_data.get('username')
password = login_form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request,user)
return redirect('home')
else:
pass
elif request.method == 'GET':

Django 28
login_form = AuthenticationForm()
return render(request,'views/login.html',{'login_form': login_form})

/src/main/templates/views/home.html

{% extends 'base/base.html' %}
{%load static%}

{% block 'title' %}
<title>CarVibe Home</title>
{% endblock %}

{% block 'body' %}
<main>
<h1>{{request.user.username}}</h1>
</main>
{% endblock %}

/settings.py

LOGIN_URL = '/login/'
LOGIN_REDIRECT_URL = '/home/'

#Django Messages
In Django, messages refer to a framework used to send temporary, one-time
notifications (like alerts, success messages, or errors) to users — often after a
form is submitted or an action is performed.

Django 29
/settings.py

MESSAGE_TAGS = {
messages.ERROR:'danger'
}

/src/users/views.py

from django.shortcuts import render , redirect


from django.http import HttpResponse
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate , login
from django.contrib import messages
# Create your views here.

def login_view(request):
if request.method== 'POST':
login_form = AuthenticationForm(request=request , data=request.POST)
if login_form.is_valid():
username = login_form.cleaned_data.get('username')
password = login_form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user is not None:
login(request,user)
messages.success(request,f'You are now logged in as {username}')
return redirect('home')
else:
messages.error (request, f'An error occured trying to login!')
else:
messages.error (request, f'An error occured trying to login!')
elif request.method == 'GET':
login_form = AuthenticationForm()
return render(request,'views/login.html',{'login_form': login_form})

/src/main/templates/base/base.html

Django 30
{%load static%}

<!DOCTYPE html>
<html lang="en" class="h-100">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="A short, accurate description of your w
<meta name="robots" content="index, follow" />

{% block 'title' %}
<title>CarVibe . List Your Car Today!</title>
{% endblock %}

<!-- Bootstrap core CSS -->


<link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">

<!-- Custom Style CSS -->


<link href="{% static 'css/style.css' %}" rel="stylesheet">

<!-- JQuery CDN -->


<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=

</head>

<body>
{% if messages %}

{% for message in messages%}


<div {% if message.tags %} class="alert alert-{{message.tags}}" {% endif
{{message}}
</div>
{% endfor %}

{% endif %}

Django 31
{% block 'body' %}
{% endblock %}
</body>

{% block 'footer' %}
<footer class="footer text-muted bg-dark py-5 ">
<div class="container">
<p class="float-end mb-1">Powered by <a href="https://www.djangopro
<p class="mb-1">CarVibe - Best Place to Buy and Sell Cars</p>
</div>
</footer>
{% endblock %}

</html>

#Creating register View


/src/users/views.py

def register_view(request):
register_form = UserCreationForm()
return render(request,'views/register.html',{'register_form': register_form})

/src/users/templates/views/register.html

{% extends 'base/base.html' %}
{% load static %}
{% load crispy_forms_tags %}

Django 32
{% block 'body' %}
<div class="text-center">
<main class="form-signin">
<form action="" method="post">
<img class="mb-4" src="{% static 'images/logo.png' %}" alt="" width
{% csrf_token %}
{{ register_form |crispy }}
<button class="w-100 btn btn-lg btn-danger" type="submit">Register<
<p class="mt-5 mb-3 text-muted">Copyright © 2025 CarVibe</p>
</form>
</main>
</div>
{% endblock %}

{% block 'footer' %}
{% endblock %}

/src/users/urls.py

from django.urls import path


from .views import login_view , register_view

urlpatterns = [
path('login/',login_view, name='login'),
path('register/',register_view, name='register'),

#Class Based View


/src/users/views.py

Django 33
from django.views import View
class RegisterView(View):
def get(self, request):
register_form = UserCreationForm()
return render(request , 'views/register.html',{'register_form':register_form}

/src/users/urls.py

from django.urls import path


from .views import login_view , RegisterView

urlpatterns = [
path('login/',login_view, name='login'),
path('register/',RegisterView.as_view(), name='register'),

#Registering Users Logic


/src/users/views.py

class RegisterView(View):
def get(self, request):
register_form = UserCreationForm()
return render(request , 'views/register.html',{'register_form':register_form}

def post(self, request):


register_form = UserCreationForm(request.POST)
if register_form.is_valid():
user = register_form.save()
user.refresh_from_db()

Django 34
login(request, user)
messages.success(
request, f'User {user.username} registered successfully.')
return redirect('home')
else:
messages.error(request, f'An error occured trying to register.')
return render(request, 'views/register.html', {'register_form': register_fo

/src/main/templates/views/main.html

{% extends 'base/base.html' %}
{% load static %}

{% block 'body' %}
<div class="d-flex h-100 text-center text-white index">
<video autoplay muted loop id="myVideo">
<source src="{% static 'videos/bg.mp4' %}" type="video/mp4">
<!-- Credit: https://www.pexels.com/@kelly-l-1179532 (Pexels.com)-->
</video>
<div class="d-flex w-100 h-100 p-3 mx-auto flex-column main-content">
<header class="mb-auto">
<div>
<h3 class="float-md-start mb-0">CarVibe</h3>
<nav class="nav nav-masthead justify-content-center float-md-end

{% if request.user.is_authenticated %}
<a class="nav-link active" aria-current="page" href="{% url 'hom
{% else %}
<a class="nav-link" href="{% url 'login' %}">Login</a>
{% endif %}

</nav>
</div>
</header>
<main class="px-3">
<h2>Find a deal near you!</h2>
<p class="lead">Looking to buy or sell a used car? We've got you
covered. </br> CarVibe is the leading used car marketplace.</p>

Django 35
<p class="lead">
<a href="{% url 'register' %}"
class="btn btn-lg btn-secondary fw-bold border-white bg-white">
</p>
</main>
<footer class="mt-auto text-white-50">
<p>Copyright © {% now 'Y' %} <a href="" class="text-white">CarVibe
class="text-white">tejth</a></p>
</footer>
</div>
</div>
{% endblock %}

{% block 'footer' %}
{% endblock %}

#Logging Out User


/src/users/views.py

@login_required
def logout_view(request):
logout(request)
return redirect('main')

/src/users/urls.py

from django.urls import path


from .views import login_view , RegisterView , logout_view

urlpatterns = [
path('login/',login_view, name='login'),
path('register/',RegisterView.as_view(), name='register'),

Django 36
path('logout/' , logout_view , name='logout'),
]

/src/main/templates/base/base.html

<div class="text-end">
<a type="button" href="{% url 'logout' %}" class="btn btn-danger">Logout<
</div>

#Creating Custom Forms


/src/main/forms.py

import re
from django import forms

from .models import Listing

class ListingForm(forms.ModelForm):
image = forms.ImageField()

class Meta:
model = Listing
fields = {'brand', 'model', 'vin', 'mileage',
'color', 'description', 'engine', 'transmisson', 'image'}

/src/main/templates/views/list.html

{% extends 'base/base.html' %}
{% load static %}
{% load crispy_forms_tags %}

{% block 'body' %}
<div class="container-fluid text-center py-5">

Django 37
<div class="container px-4 py-5" id="hanging-icons">
<div class="row g-4">
<div class="col d-flex align-items-start">
<div>
<h2 class="mb-3 border-bottom" style="color: black">List</h2>
</div>
</div>
</div>
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="row">
<div class="col-6">
{{listing_form|crispy}}
</div>
<div class="col-6">
{{location_form|crispy}}
</div>
</div>
<button class="btn btn-lg btn-danger" type="submit">Save</button>
</form>
</div>
</div>
{% endblock %}

#Django Filters(Dont forget to add it inside


settings.py)
/main/filters.py

import django_filters

from .models import Listing

Django 38
class ListingFilter(django_filters.FilterSet):

class Meta:
model = Listing
fields = {'transmisson': ['exact'], 'brand': [
'exact'], 'model': ['icontains'], 'mileage': ['lt']}

/main/views.py

@login_required
def home_view(request):
listings = Listing.objects.all()
listing_filter = ListingFilter(request.GET, queryset=listings)
user_liked_listings = LikedListing.objects.filter(
profile=request.user.profile).values_list('listing')
liked_listings_ids = [l[0] for l in user_liked_listings]
context = {
'listing_filter': listing_filter,
'liked_listings_ids': liked_listings_ids,
}
return render(request, "views/home.html", context)

/main/home.html

{% extends 'base/base.html' %}
{%load static%}
{% load crispy_forms_tags %}

{% block 'title' %}
<title>CarVibe Home</title>
{% endblock %}

{% block 'body' %}
<main>

Django 39
<section class="py-5 container">
<div class="row py-lg-5">
<div class="col-lg-6 col-md-8 mx-auto">
<h1 class="fw-light">CarVibe</h1>
<p class="lead text-muted">The best place to buy or sell modern ent
made it that much easier.</p>
<a href="{% url 'list' %}" class="btn btn-primary my-2">List Your Car
</div>
<div class="col-lg-6 col-md-8 mx-auto">
<form method="get">
{{ listing_filter.form|crispy }}
<button class="btn btn-sm btn-danger" type="submit">Submit</bu
</form>
</div>
</div>
</section>
<div class="album py-5 bg-light">
<div class="container">
<div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 g-3">
{% for listing in listing_filter.qs %}
<div class="col">
{% include "components/listing_card.html" %}
</div>
{% endfor %}
</div>
</div>
</div>
</main>
{% endblock %}

#Django URL Parameters


🔗 Django URL Parameters – Explained Simply
Django 40
Django URL parameters are dynamic parts of the URL that allow you to pass
data directly through the URL to your views.

✅ 1. Positional Parameters (Path Converters)


Used to capture values from the URL and pass them as arguments to views.

# urls.py
from django.urls import path
from . import views

urlpatterns = [
path('car/<int:car_id>/', views.car_detail, name='car_detail'),
]

# views.py
def car_detail(request, car_id):
return HttpResponse(f"Car ID is {car_id}")

/car/7/ → car_id = 7

Common converters:

Converter Description
int Matches integers
str Matches any string
slug Matches slug strings (letters, numbers, dashes)
uuid Matches UUIDs
path Like str , but allows /

✅ 2. Keyword Query Parameters ( ?key=value )


These are sent in the URL after a ? , and accessed using request.GET .

/search/?q=BMW

Django 41
# views.py
def search(request):
query = request.GET.get('q')
return HttpResponse(f"Searching for: {query}")

Summary:
Type Example URL Access in View

Path Parameter /car/12/ def view(request, car_id)

Query Param /search/?q=BMW request.GET.get('q')

#Getting and Posting Data in Custom


Forms
@method_decorator(login_required, name='dispatch')
class ProfileView(View):

def get(self, request):


user_listings = Listing.objects.filter(seller=request.user.profile)
user_liked_listings = LikedListing.objects.filter(
profile=request.user.profile).all()
user_form = UserForm(instance=request.user)
profile_form = ProfileForm(instance=request.user.profile)
location_form = LocationForm(instance=request.user.profile.location)
return render(request, 'views/profile.html', {'user_form': user_form,
'profile_form': profile_form,
'location_form': location_form,
'user_listings': user_listings,
'user_liked_listings': user_liked_listings, })

def post(self, request):


user_listings = Listing.objects.filter(seller=request.user.profile)
user_liked_listings = LikedListing.objects.filter(

Django 42
profile=request.user.profile).all()
user_form = UserForm(request.POST, instance=request.user)
profile_form = ProfileForm(
request.POST, request.FILES, instance=request.user.profile)
location_form = LocationForm(
request.POST, instance=request.user.profile.location)
if user_form.is_valid() and profile_form.is_valid() and location_form.is_valid
user_form.save()
profile_form.save()
location_form.save()
messages.success(request, 'Profile Updated Successfully!')
return redirect('profile')
else:
messages.error(request, 'Error Updating Profile!')
return render(request, 'views/profile.html', {'user_form': user_form,
'profile_form': profile_form,
'location_form': location_form,
'user_listings': user_listings,
'user_liked_listings': user_liked_listings, })

class UserForm(forms.ModelForm):
username = forms.CharField(disabled=True)

class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email')

class ProfileForm(forms.ModelForm):
photo = forms.ImageField(widget=CustomPictureImageFieldWidget)
bio = forms.TextInput()

class Meta:
model = Profile
fields = ('photo', 'bio', 'phone_number')

Django 43
#Custom Form Widget
🧩 What is a Custom Form Widget in Django?
In Django, a form widget is the part of the form responsible for rendering HTML
input elements like <input> , <select> , <textarea> , etc.
A custom form widget allows you to:

Modify how a field is displayed in HTML (e.g., custom classes,


placeholders, input type).

Add custom JavaScript or CSS.

Change rendering behavior.

✅ Why Use a Custom Widget?


To:

Add custom HTML attributes (like class , id , placeholder )

Render fields as date pickers, sliders, color pickers, etc.

Integrate with third-party JavaScript libraries (e.g., DatePicker , Select2 , etc.)

🛠️ Example: Creating a Custom Widget


1. Custom Widget for a TextInput with a CSS class

from django import forms

class CustomTextInput(forms.TextInput):
def __init__(self, *args, **kwargs):
kwargs.setdefault('attrs', {}).update({'class': 'custom-input', 'placehold
er': 'Enter text...'})
super().__init__(*args, **kwargs)

2. Using it in a form

Django 44
class MyForm(forms.Form):
name = forms.CharField(widget=CustomTextInput())

This renders:

<input type="text" class="custom-input" placeholder="Enter text..." name="n

Want to go further?
You can even override:

render() – for full HTML control

value_from_datadict() – for custom data cleaning/parsing

Add custom JS via Media class

#Environment Variable
🌍 What are Django Environment Variables?
Environment variables in Django are used to keep sensitive or environment-
specific information (like secrets, database passwords, or debug flags) outside
of your source code.
Instead of hardcoding values in settings.py , you load them from a .env file or your
system's environment. This keeps your code secure, portable, and clean.

✅ Why Use Environment Variables?


Without Env Vars ( ❌ Bad Practice) With Env Vars ( ✅ Best Practice)
SECRET_KEY = "abc123" SECRET_KEY = env('SECRET_KEY')

DEBUG = True DEBUG = env.bool('DEBUG', False)

Hard to manage across servers Easy to manage and override

Security risk if pushed to GitHub Secrets stored locally or on server only

Django 45
pip install django-environ

2. 🗂 Create a .env file in your project root (same folder as manage.py )

# .env
SECRET_KEY=your-very-secret-key
DEBUG=True
DATABASE_NAME=mydb
DATABASE_USER=myuser
DATABASE_PASSWORD=mypass

3. 🧠 Update settings.py
Add this at the top of settings.py :

import environ
import os
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

# Initialize environment
env = environ.Env(
DEBUG=(bool, False)
)

# Load .env file


environ.Env.read_env(os.path.join(BASE_DIR, '.env'))

# Use variables
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG')

Django 46

You might also like