Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

For Django

We need framework for interface between frontend and


backend.
You first have to install anaconda python
Than create virtual env using conda
This will install python for that particular env
Create env using
conda create --name env_name django
For this refer udemy material
You can also refer
Conda website documentation for more details.
Then after install Django
You can also specifically mention version number
Ex
Python=3.5 in conda.. syntax or any.

For activate and deactivate env


For windows
Activate envname
For mac or linux
source deactivate envname

For deactivate, windows


deactivate envname
For mac or linux
source deactivate envname

For check env name use


conda info envs
so that means everything we work is in that particular
virtual environment
so we also use different versions of python in different
environment.

Django Project
commands
django-admin startproject proj_name

this create folder


hierarchy
proj_name
proj_name
_init_.py
Settings.py
Urls.py
Wsgi.py
manage.py
For What all files do check udemy material pdf Django part 2
project.
__init__.py beacause of its name __init__ python will treat it as
package.
__settings__.py this will mange all the settings of web
application
__urls__.py this file save all urls pattern and different pages of
web application and how it connect to end user.
__wsgi__.py this file is a python script that act as a web server
gateway interface. It used us to deploy web app to production.
Manage.py this script asscociated with many commands as we
build our web app.

python manage.py runserver

when we fire this command deployment server is given like


http://127.0.0.1:8000/ copy and paste it in browser .
*for it run you have to fire python manage.py runserver and dont quit it for
run this.
this is your web app locally hosted on your computer.

python manage.py startapp app_name


it will create app_name folder and it have some files
admin.py here you acan register models and then Django use it
as admin interface.
Apps.py here you can place application specific configuration
Models.py you can store applications data model.
Tests.py here you can store test functions to test your code.
Views.py here you have functions that handle requests and
return response
Migration folder:
This directory stores database specific information as it
relates to the model.

Now edit views.py


As we know this file handle request and response then
from django.http import HttpResponse
and function for response.

now map this view.py to url.py file.


There are many methods for url mapping
Method 1
from first_app import views
and add this url pattern in to urlpatterns
url(r'^$',views.index,name=index),
here view.index is index is function name of views.py
which can be any function name that is in views.py
Method 2
from Django.conf.urls import include

and add this into urlpatterns=[..]


url(r^my_new extension/,include(first_app.urls)),
here mynew extension can be anything we wanted to be in url search
path
that means,
if you will type this my_new extension/ you will redirect to
views.help_page and help_page is a function of views.py and that
contain template tag pf html file.
create urls.py file in first_app folder
and instert this in this file.
from Django.conf.urls import url
from first_app import views

and add this url pattern in to urlpatterns


url(r'^$',views.index,name=index),

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
}]

DIRS:defines a list of directories where the engine


should look for template source files, in search
order.
Views.py
If import render is not here than we have to import it.
Add dictionary in index function and return it using render
def index(request):
my_dict = {'insert_me': "Now I am coming from
first_app/index.html!"}
return
render(request,'first_app/index.html',context=my_dict)

so in render function parameters are ..


function parameter-request
html file- index.html
dictionary name context=my_dict

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

Inject css file


Add css file into static/css folder
Assume that we have loaded static file after
doctype tag
Write css code into this file and inject it into html
file in templates/first_app/index.html
Use link tag in head tag of html and add template
tags in href in head tag,
<link rel="stylesheet"
href="{% static "css/mystyle.css" %}"/>

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

For more detal check Django command word


file.
Populating script
Crete script that will populate our model with
dummy data.
Here there are many fake function in faker
Like fake url
Fake company
Fake credit card
For this check Django official documentation
We are considering models.py file for this file.

Model Template Views


First create models-add it to views.py- add
view.py variable into html file using template tags.

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>

Name=thankyou is in url file.


Method 2
<a href={ % url basicapp.views.thankyou%}>thanks</a>

Method 3 Use this method always

<a href={%url basicapp:thankyou%}thanks</a>


This method require app_name variable in urls.py

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.

o Main steps for inheritance:


Find repetitive part of your project
Like navbar,pictures,etc.
Create base template of them
Set tags in the base template
Extend and call those tags anywhere.

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 %}

Template Filters and custom Filters


It allows you to effect the injection before displaying it to user.
Syntax of template filter:
{{ django | title}}
{django: the web framework for perfectionists}
And
title is command for first letter capital.
Here Django is a dictionary means key value pair and pass this keydjango to filer.
Title is command which takes first letter of word capital.

Use this link for templates,


docs.djangoproject.com/en/1.10/topics/templates/#filters
or
google search:
Django+templates

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

You might also like