django code 2 pg 10 to 20

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

How to Add CSS, Images and Java Script and templates inside applica on

1. Django-admin startproject ucs6


2. Cd ucs6
3. Python manage.py startapp course
4. Python manage.py startapp fees
5. Now install app
6. Se ngs.py

from pathlib import Path


import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES_DIR=os.path.join(BASE_DIR,'templates')
STATIC_DIR=os.path.join(BASE_DIR,'static')
.
.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'course',
'fees',
]

.
.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TEMPLATES_DIR],
'APP_DIRS': True,
.
.
.
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/

STATIC_URL = 'static/'
STATICFILES_DIRS=[STATIC_DIR]

# Default primary key field type


# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
7. Urls.py (ucs6)

from django.contrib import admin


from django.urls import path, include
from . import views

urlpatterns = [
path('admin/', admin.site.urls),
path('course/',include('course.urls')),
path('fees/',include('fees.urls')),
path('',views.home),
]
8. Views.py (ucs6)

from django.shortcuts import render

def home(request):
return render(request, 'home.html',{'data':'Home Page'})
9. Create templates folder in ucs6
10. home.html (ucs6/templates)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home</title>
</head>
<body>
<h1>Welcome to {{data}}</h1>
</body>
</html>
11. Create sta c folder inside ucs6
12. Create css, images and js folder inside sta c
13. Create style.css in css folder

img{
width: 350px;
height: 350px;
border: solid 2px;
border-radius: 50%;

}
body{
background-color: cadetblue;
}
14. Copy two images in images folder named dj1.png and dj2.jpeg for this project.
15. Create all.js file in js folder

function display(){
alert("Main Java Script Hoon!!!")
}
16. Urls.py (course)

from django.urls import path


from . import views

urlpatterns = [
path('learn_dj/',views.learn_django),
path('login/',views.tut_django,name='tutdj'),

]
17. Views.py(course)

from django.shortcuts import render

# Create your views here.


def learn_django(request):
return render(request,'course/info.html',{'data':'Django Course Page'})

def tut_django(request):
return render(request,'course/tutorials.html',{'data':'Django Tutorials
Page'})
18. Create templates/course (course)
19. Info.html (course/templates/course)

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{data}}</title>
<style>
body{
background-color: rgb(255, 242, 127);
}
</style>

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


</head>
<body>
<h1>Welcome to {{data}}</h1>
<table width="100%" cellpadding="10">
<tr bgcolor="blue" >
<td><font color="white" size="4" face="arial">0562-2520075 ,
2858616 | [email protected] | Announcement: Online Admission
Form</font></td>
</tr>
</table>
<img src="{% static 'images/dj1.png' %}">
<img src="{% static 'images/dj2.jpeg' %}">
<p style="color: brown;">Lorem ipsum dolor sit amet consectetur
adipisicing elit. Ipsam ratione, doloremque consequatur distinctio pariatur
quaerat magni minus! Ea, provident veniam!</p>

<hr>
<a href="{% url 'tutdj' %}">Django Tutorial</a><br>
<a href="{% url 'tutdj' %}">Django Tutorial</a><br>
<a href="{% url 'tutdj' %}">Django Tutorial</a><br>
<a href="{% url 'tutdj' %}">Django Tutorial</a><br>

<hr>
<form action="">
<input type="button" value="Click Me" onclick="display()" >
</form>

<script src="{% static 'js/all.js' %}"></script>

</body>
</html>
20. Tutorials.html (course/templates/course)

<!DOCTYPE html>
{% load static %}
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{data}}</title>
<style>
body{
background-color: rgb(255, 242, 127);
}
</style>

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

</head>
<body>
<h1>Django Tutorials</h1>
<hr>

<p style="color: brown;">Lorem ipsum dolor sit amet consectetur


adipisicing elit. Ipsam ratione, doloremque consequatur distinctio pariatur
quaerat magni minus! Ea, provident veniam!</p>
<P>Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit
sapiente ducimus esse id, illo magni. Odio architecto earum voluptatem autem
fugiat placeat impedit, mollitia cupiditate ratione dolorem animi, nesciunt
ex. Necessitatibus itaque ipsam, eum quo quas voluptatum excepturi nam in,
temporibus minus asperiores aliquam impedit. Provident at aut totam, quia
consequatur sequi soluta quisquam voluptatibus dolorum laboriosam voluptas?
Possimus ab sed voluptas in officia quibusdam quis delectus corrupti tenetur
quaerat, aliquam itaque iusto expedita! Autem ea facilis neque veniam,
inventore deserunt ratione ipsam. Hic in quasi eius dignissimos quo repellat
recusandae commodi accusantium nesciunt minima aperiam ea, dolorum modi vel,
temporibus eos saepe ad illo qui quod rem? Dicta numquam nihil fuga earum
corporis perspiciatis, culpa ipsa dolores, maiores tenetur, eius sint facere
asperiores enim minima eos itaque laudantium. Vero, eligendi. Voluptatibus
rem dignissimos ullam recusandae quas mollitia, voluptates minima ea fugit
nisi illum molestias doloribus quia id corrupti labore. Tenetur voluptate
recusandae illo commodi iste porro asperiores molestiae esse perspiciatis nam
laboriosam veritatis ipsa, explicabo, at nobis nulla libero accusamus,
aspernatur rem saepe! Nesciunt quo, placeat iusto distinctio tenetur
excepturi totam aliquid veritatis nulla officia voluptatem amet quia voluptas
aliquam atque voluptatum. Ad nemo rem laboriosam beatae, numquam
recusandae.</P>

<hr>
<a href="../learn_dj">Learn Django</a><br>

</body>
</html>
21. Urls.py (fees)

from django.urls import path


from . import views
urlpatterns = [
path('feesdj/',views.fees_django),
]
22. Views.py (fees)

from django.shortcuts import render

# Create your views here.


def fees_django(request):
return render(request,'fees/info.html',{'data':7500})
23. Info.html (fees/templates/fees)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{data}}</title>
</head>
<body>
<h1>Welcome to {{data}}</h1>
</body>
</html>
Django Template Language

1. Django-admin startproject ucs8


2. Cd ucs8
3. Python manage.py startapp course
4. Now install app and create templates
5. Se ngs.py

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
.
.
.
'DIRS': [TEMPLATE_DIR],

6. Urls.py (ucs8)

from django.contrib import admin


from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('cor/', include('course.urls')),
]
7. Urls.py (course)

from django.urls import path


from . import views
urlpatterns = [
path('learndj/', views.learn_django),
]
8. Views.py

from django.shortcuts import render


from datetime import datetime
# Create your views here.

#Example 1
def learn_django(request):
return render(request, 'course/courseone.html', {'nm':'Django'})

"""
#Example 2
def learn_django(request):
cname = 'Django'
duration = '4 Months'
seats = 10
django_details = {'nm':cname, 'du':duration, 'st':seats}
return render(request, 'course/courseone.html', django_details)
"""
"""
#Example 3
def learn_django(request):
return render(request, 'course/courseone.html', {'nm':'Django is
awesome'})
"""
"""
#Example 4
def learn_django(request):
d = datetime.now()
return render(request, 'course/courseone.html', {'dt':d})
"""
"""
#Example 5
def learn_django(request):
return render(request, 'course/courseone.html', {'p1':56.24321,
'p2':56.000, 'p3':56.37000})
"""
"""
#Example 6
def learn_django(request):
return render(request, 'course/courseone.html', {'nm':True})
"""
"""
#Example 7
def learn_django(request):
return render(request, 'course/courseone.html', {'nm':'Django'})
"""
"""
#Example 8
def learn_django(request):
return render(request, 'course/courseone.html', {'nm':'Django',
'st':5})
"""
"""
#Example 9
def learn_django(request):
student = {'names': ['Rahul', 'Sonam', 'Raj','Anu']}
return render(request, 'course/courseone.html', student)
"""
"""
#Example 10
def learn_django(request):
stu = {'stu1': {'name': 'Rahul', 'roll':101},
'stu2': {'name': 'Sonam', 'roll':102},
'stu3': {'name': 'Raj', 'roll':103},
'stu4': {'name': 'Anu', 'roll':104},
}
students = {'student':stu}
return render(request, 'course/courseone.html', students)
"""
"""
#Example 11
def learn_django(request):
data = {'name': 'Rahul', 'roll':101}
return render(request, 'course/courseone.html', {'data':data})
"""

"""
#Example 12
def learn_django(request):
data = {'stu1': {'name': 'Rahul', 'roll':101},
'stu2': {'name': 'Sonam', 'roll':102},
'stu3': {'name': 'Raj', 'roll':103},
'stu4': {'name': 'Anu', 'roll':104},
}
return render(request, 'course/courseone.html', {'data':data})
"""
9. Templates/course/courseone.html

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
body {
background-color: black;
color: seashell;
}
</style>
<title>Django</title>
</head>
<body>
<h2>Example 1</h2>
<h3>Hello {{nm}}</h3>

<!-- <h2>Example 2</h2>


<h3>Name: {{nm}} Duration: {{du}} Total Seats: {{st}}</h3> -->

<!-- <h2>Example 3.1</h2>


<h3>{{nm|capfirst}}</h3> -->

<!--<h2>Example 3.2</h2>
<h3>{{nm|default:'nothing'}}</h3> -->

<!-- <h2>Example 3.3</h2>


<h3>{{nm|length}}</h3> -->

<!-- <h2>Example 3.4</h2>


<h3>{{nm|lower}}</h3> -->

<!-- <h2>Example 3.5</h2>


<h3>{{nm|upper}}</h3> -->

<!-- <h2>Example 3.6</h2>


<h3>{{nm|slice:"2"}}</h3> -->

<!-- <h2>Example 3.7</h2>


<h3>{{nm|truncatechars:3}}</h3> -->

<!-- <h2>Example 3.8</h2>


<h3>{{nm|truncatewords:2}}</h3> -->

<!-- <h2>Example 4.1</h2>


<h3>{{dt}}</h3>

<h2>Example 4.2</h2>
<h3>{{dt|date:"D d M Y"}}</h3>

<h2>Example 4.3</h2>
<h3>{{dt|time:"H:i"}}</h3>

<h2>Example 4.4</h2>
<h3>{{dt|date:"SHORT_DATE_FORMAT"}}</h3>

<h2>Example 4.5</h2>
<h3>{{dt|time:"TIME_FORMAT"}}</h3> -->

<!-- <h2>Example 5.1</h2>


<h3>{{p1}}</h3>
<h3>{{p2}}</h3>
<h3>{{p3}}</h3>

<h2>Example 5.2</h2>
<h3>{{p1|floatformat}}</h3>
<h3>{{p2|floatformat}}</h3>
<h3>{{p3|floatformat}}</h3>

<h2>Example 5.3</h2>
<h3>{{p1|floatformat:3}}</h3>
<h3>{{p2|floatformat:3}}</h3>
<h3>{{p3|floatformat:3}}</h3>

<h2>Example 5.4</h2>
<h3>{{p1|floatformat:0}}</h3>
<h3>{{p2|floatformat:0}}</h3>
<h3>{{p3|floatformat:0}}</h3>

<h2>Example 5.5</h2>
<h3>{{p1|floatformat:-3}}</h3>
<h3>{{p2|floatformat:-3}}</h3>
<h3>{{p3|floatformat:-3}}</h3> -->

<!-- <h2>Example 6</h2>


{% if nm %}
<h3>Hello</h3>
{% endif %} -->

<!-- <h2>Example 7</h2>


{% if nm %}
<h3>Hello {{nm}}</h3>
{% endif %} -->

<!-- <h2>Example 8.1</h2>


{% if nm and st %}
<h3>{{st}} seats are available for {{nm}} course</h3>
{% endif %}

<h2>Example 8.2</h2>
{% if nm or st %}
<h3>{{st}} seats are available for {{nm}} course</h3>

You might also like