0% found this document useful (0 votes)
62 views

Django Notes

django notes

Uploaded by

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

Django Notes

django notes

Uploaded by

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

execmd to create app:

1) django-admin startapp appname


(OR)
2) python manage.py startapp appname

url:

1) : from app import views


2) : path('sam/',views.(function name created in views.py))

template import :

1)import os

2)TEMPLATES_DIRS=os.path.join(BASE_DIR,'templates')

3)scroll down and insert TEMPLATES_DIRS in template [.... variabel ], like 'DIRS':
[TEMPLATES_DIRS]

to write html files :

1) create a html file in template and write something


2) go to views and write a def function like

def sample(request):
return render(request,'html file name')
3) go to url and create a new path to call function

DTL

simple if :-

{% if <cmd> %}
TSB
{% endif %}

else :-

{% if <cmd> %}
TSB
{% else %}
FSB
{% endif %}

elif :-
{% if <cmd> %}
TSB
{% elif <condition> %}
TSB2
.
.
.
{% else %}
FSB
{% endif %}

take one number from browser to check if it is even or odd

for loop :-

{% for var in collection %}


STATEMENT BLOCK
{% endfor %}

jinja url tag:=

url tag="{% url, 'mappingname' %}"

if mapping names are same then we use {% "appname:mappingname" arg1=val arg2=val %}


if we use above syntax we have to use app_name = 'appname' in urls page

TEMPLATE INHERITENCE :-

{% extends 'template-name' %}

{% block name %}

block

{% endblock %}

PARTIALS :-

{% include 'partial-name' %}

STATIC FILES :-
These are the files which can not be modified by the server.
example:-
css,js,gif,fonts,images

MODELS :-

TO OPEN PYHTON SHELL WE USE python manage.py shell

TO INSERT DATA

first= from app1.models import Customer

syntax to insert = 1) obj= Customer()

First syntax:-

obj=Customer(cname='jusrtoj'.....)
then obj.save() to save the data

second syntax:-

>>>
Customer.objects.create(cname='mohan',Email='bandly@gmail.com',Mobile=9999999999,lo
cation='thulali')

third method(to create bulk data) :-

Customer.objects.bulk_create([ob1,obj2])

TO READ THE RECORDS WE HAVE THREE METHODS :-

1) To read all the records from the table :- data = modelname.objects.all()


2) To read a single record from the table :- data =
modelname.objects.get(argument=value)[we have one drawback here so we use filter
method]
3) Reading data using filter method :- data =
modelname.objects.filter(argument=value)

TO UPDATE THE RECORDS :-

1) data = modelname.objects.filter(argu=value) //filter is the most prefered


method to update the data
data.update(column=new_value)
NOTE :- if use get method then we will get a error, we can also use all to update
all the records.

TO DELETE A RECORD FROM THE TABLE :-

data= Customer.objects.get(id=3)
data.delete()

homework :- create a table called employee with ename, age, sal, loc, designations

OPERATORES:-

COMPARISION / RELATIONAL OPERATORS:

syntax = data=Employee.objects.filter(columnname _ _gt=value) //FOR


GREATER THAN
data=Employee.objects.filter(columnname _ _lt=value) //FOR
LOWER THAN
data=Employee.objects.filter(columnname _ _gte=value)
//greater than equal to
data=Employee.objects.fliter(columnname _ _lte=value) //lower
than equal to

data=Employee.objects.fliter(columnname=value) //FOR EQUAL


TO
data=Employee.objects.exclude(columnname=value) //FOR NOT
EQUAL TO

RANGE :-
syntax : data=Employee.objects.filter(columnname _ _range=(start,end))
//FOR RANGE

EXAMPLE : >>> data=Employee.objects.filter(age__range=(25,35))

in :-
syntax: data=Employee.objects.filter(columnname _
_in=[val1,val2,val3]) //FOR IN

EXAMPLE : >>> data=Employee.objects.filter(age__in=[21,23,40])

FOR NOT IN JUST USE EXCLUDE IN THE PLACE OF FILTER

CONTAINS :-
syntax: data=Employee.objects.filter(columnname _
_contains='val') //FOR CONTAINES

EXAMPLE: >>>
data=Employee.objects.filter(ename__contains='us')
SATRT WITH :-(select all the names starting with a)

syntax: data=Employee.objects.filter(columnname _
_startwith='a') //NAMES STARTING WITH 'j'

EXAMPLE : >>>
data=Employee.objects.filter(ename__startswith='j')

LOGICAL OPERATORS:

AND :-

[METHOD 1]
syntax : data=Employee.objects.filter(columnname _ _contains='val') &
data=Employee.objects.filter(columnname _ _gt=value)

[METHOD 2] //USING Q CLASS

syntax : from django.db.models import Q


data=Employee.objects.filter( Q(arg=val) & Q(arg=val) )

EXAMPLE : >>> data=Employee.objects.filter(Q(ename__contains='j') &


Q(age__gte=23))

[METHOD 3]

syntax : data=Employee.objects.filter(arg=val1, arg2=val2, arg=3=val3.....)

EXAMPLE: >>> data=Employee.objects.filter(ename__contains='j',


age__gte=23,......)

OR :-

syntax : data=Employee.objects.filter(columnname _ _contains='val') |


data=Employee.objects.filter(columnname _ _gt=value)

IF WE USE Q CLASS:
from django.db.models import Q
data=Employee.objects.filter( Q(arg=val) |
Q(arg=val) )

NOT :-

i) syntax : data=Employee.objects.exclude(arg=val)

BY USIN Q CLASS
ii) syntax : data=Employee.objects.filter(~Q(arg=val)

MULTI-ROW FUNCTIONS :-

To use multi-row functions first we have to use this


syntax : from django.db.models import Min, Max, Avg, Sum, Count

example : >>> data = Employee.objects.all().aggregate(Max('sal'))


>>> data = Employee.objects.all().aggregate(Min('sal'))
>>> data = Employee.objects.all().aggregate(Avg('sal'))
>>> data = Employee.objects.all().aggregate(Sum('sal'))
>>> data = Employee.objects.all().aggregate(Count('sal'))

ORDER BY CLAUSE :-
[FOR ASCENDING ORDER]

syntax : data = Employee.objects.all().order_by('sal')

NOTE : Since we get data in query set we have to use for : for i
in data:
print(i.sal)

[FOR DECSENDING ORDER]

syntax : data = Employee.objects.all().order_by('-sal')

TO TAKE INPUTS FROM BROWSER AND STORE IT INTO DATA BASE USE FOLLOWING STEPS :-

1) in views import : from django.http import HttpResponse


from app1.models import Employee

create a function in views like,

def Employee(request,ename,age,sal,designations):

Employee.objects.create(ename=ename,age=age,sal=sal,designations=desigantions)
return HttpResponse("The records has been saved.")

2) in url :

from app1 import views

path('emp/<ename>/<int:age>/<int:sal>/<designations>/',views.EmployeeVW),

That's all.

ASSIGNMENTS:

create function to read a single data from browser url


create function to read a all data from browser url
create function to update sal from browser url
create function to delete data from browser url using exception handling
MODEL INHERITENCE :

1) Single Inheritence
2) Multi-level inheritence
3) multilevel inheritence
3) heirarchial inheritence
4) hibrid inheritence

important:-

5) abstract model
6) proxy model

MY SQL CONNECTION:

to show databases : show databases;


to show tables : show tables;
to create database : create database database1;
to insert data to a particular database : use database1;
to describe table : desc app1_sample;

in django settings.py make changes in database, for example :

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'database1',
"USER":'root',
"PASSWORD" :'root',
"PORT":'3306',
"HOST":'localhost',
}

create a class in models

to use makemigrations we have to install pymysql : pip install mysqlclient

now use python manage.py makemigrations to insert table into mysql database

to describe table : desc app1_sample;

ABSTRACT MODEL:

class Detail(models.Model):
name = models.CharField(max_length=20)
mob = models.PositiveBigIntegerField(null=True)
email = models.EmailField(null=True)
loc = models.CharField(max_length=20,null=True)

class Meta:
abstract=True

class Student(Detail):
st_id=models.CharField(max_length=10, primary_key=True)
st_name = models.CharField(max_length=20,null=False)
fee= models.PositiveBigIntegerField(null=True)

class Teacher(Detail):
te_id=models.CharField(max_length=10,primary_key=True)
te_name=models.CharField(max_length=20,null=True)
t_pho=models.PositiveBigIntegerField(null=True)

PROXY MODEL :

class Sample(models.Model):
name = models.CharField(max_length=20)
mob = models.PositiveBigIntegerField()
email = models.EmailField(null=True)
loc = models.CharField(max_length=20)

class Indexdb(Sample):
class Meta:
proxy=True

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy