Django
Django
Django
Django Project
commands
django-admin startproject proj_name
basically this will allow any url which has url pattern like
www.django.com/first_app
www.palak.com/first_app
every url which has /first_app
Django templates
Template variable allows us to inject content into html
directly from Django.
Inside html we inject template.
Hierarchy:
f_project/templates/f_app/index.html
settings.py
now add or join template to base directory means main folder so add
this after
BASE_DIR=os.path.dirname.., for run on any operating system
TEMPLATE_DIR=os.path.join(BASE_DIR,"templates")
Now ,
Add TEMPLATE_DIR into TEMPLATES=[{..dIR=[]}]
TEMPLATES=
[{
DIRS:[TEMPLATES_DIR,] //add this variable
name only
}]
Static files
Inside project folder create static folder same as we
create template folder inside create css and images
folder.
First_project/static/images
First_project/static/css
Here template tag is {% %} for complex injection
and logic
Same as template modify settings.py for static files.
Settings.py
now add or join static to base directory means main folder so add this
after
BASE_DIR=os.path.dirname..,in settings.py
for run on any operating system
STATIC_DIR= os.path.join(BASE_DIR,"static")
Add STATICFILES_DIRS after STATIC_URL=/static/
line, add list and variable that we created
STATIC_DIR
STATICFILES_DIRS=[
STATIC_DIR,
]
Now add image into images folder.
After this modify templates/first_app/index.html
file.
Add {% load staticfiles %} after doctype html tag
and before html tag.
{% load staticfiles %}
After this for image injecting add img tag and in src
add template tag.
<img src="{% static "images/surat.jpg" %}" alt="">
Now try to run using python manage.py runserver
Models
Django uses models to incorporate with databases.
Django comes with SQLite but we can change backends.
So create models in models.py
Add demo data using python shell but we will not add all
data using python shell
We will use admin interface for this.
Admin interface
Here you can add data
Delete data
Create more users
Change admin password
For this,
First runserver and 127.0.0.1/admin
Forms
Here we are using forms for taking user input and
after retrieve.
Its coding is similar like models.py
form.as_p forms in paragraph tag
HTTP: it is design to enable communaction between
a client and a server.
GET and POST are request response protocol:
GET: request data from resource.
POST: submit data to be process to a resource.
CSRF :
Cross Site request forgery. Token
{% csrf_token %}
This is security measures.
Which secures HTTP POST action that is
initiated on the subsequent submission of form.
In Django CSRF is compulsory to run form.
Relative url template
We need this bcz our hard codoed path in anchor tag
will not run on other system.
How to replace this,
<a hrefbasicapp/thankyou>thanks</a>
Can be change to,
Can be change to,
Method 1
<a href={% url thankyou%}>thanks </a>
Template inheritance:
it allows us to create base template from which we can inherit.
It saves a lot of repetitive work and much easier to maintain the same
base look and feel across our entire website.
<!DOCTYPE html> tag is compulsory.
Base.html
{% block body_block %}
{# Anything outside of this will be inherited if you use extend.#}
{% endblock %}
other.html
{% extends "basic_app/base.html" %}
{% block body_block %}
<h1>This is an example of template inheritance!</h1>
<h2>Officially this is the other.html page!</h2>
{% endblock %}
create folder in app folder and add file __init__.py inside it.
basic_app/templatetags/__init__.py
__init__.py tell django or python that this is a model package.
__init__.py file is compulsory to create for below file.
Now create another file,
basic_app/templatetags/my_extras.py
we have built in filters and we can also create custom template.
Check Django command my notes.
User Authentication:
We will focus on built in tools of Django:
users and user model
permissions
groups
password and authentication
logging and logout
Passwords:
We will use default PBKDF2 algorithm with SHA256 hash
that is built in Django.
Secure Hash Algorithm
For check how SHA 256 work check:
www.xorbin.com/tools/sha256-hash-calculator