Django Notes
Django Notes
Django Notes
url:
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]
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 %}
for loop :-
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 INSERT DATA
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')
Customer.objects.bulk_create([ob1,obj2])
data= Customer.objects.get(id=3)
data.delete()
homework :- create a table called employee with ename, age, sal, loc, designations
OPERATORES:-
RANGE :-
syntax : data=Employee.objects.filter(columnname _ _range=(start,end))
//FOR RANGE
in :-
syntax: data=Employee.objects.filter(columnname _
_in=[val1,val2,val3]) //FOR IN
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 3]
OR :-
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 :-
ORDER BY CLAUSE :-
[FOR ASCENDING ORDER]
NOTE : Since we get data in query set we have to use for : for i
in data:
print(i.sal)
TO TAKE INPUTS FROM BROWSER AND STORE IT INTO DATA BASE USE FOLLOWING STEPS :-
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 :
path('emp/<ename>/<int:age>/<int:sal>/<designations>/',views.EmployeeVW),
That's all.
ASSIGNMENTS:
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:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'database1',
"USER":'root',
"PASSWORD" :'root',
"PORT":'3306',
"HOST":'localhost',
}
now use python manage.py makemigrations to insert table into mysql database
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