Lecture 07

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

ITC3304

WEB APPLICATION DEVELOPMENT

H. U. Yakubu - CS Department, BUK. (Lecture 7) 1


Content
 Templates Inheritance and Reusability
 Template Tags and Variables
 CRUD Operations

H. U. Yakubu - CS Department, BUK. (Lecture 7) 2


Templates Inheritance

H. U. Yakubu - CS Department, BUK. (Lecture 7) 3


Template Inheritance
 Django is a popular framework which allows you to achieve
more with less code by providing ready-made apps.
 One of its most powerful features helps you easily use one
template across all your pages in a web app.

H. U. Yakubu - CS Department, BUK. (Lecture 7) 4


Template Inheritance Contd.
 Let’s say that we have a website which comprises of multiple pages
— the ‘home’, ‘departments’, students’, and many other pages.
 The common components between the pages are the header and
the footer. One way of going about this is having separate html
files, one for each page. However, it would be a bad practice to
copy and paste header and footer code to all the pages.
 Furthermore, if we have to make changes to the footer, we will
have to change it in all the files, which is not in accordance with
the DRY (Don’t Repeat Yourself) principle of Django.

H. U. Yakubu - CS Department, BUK. (Lecture 7) 5


Template Inheritance Contd.
 Django allows us to avoid that by creating a ‘base’ template
which contains the common components and inheriting that
template in all our other templates.
 Template inheritance allows you to build a base (skeleton)
template that contains all the common elements of your site
and defines blocks that child templates can override.

H. U. Yakubu - CS Department, BUK. (Lecture 7) 6


Home Page
<!DOCTYPE html>
<html lang="en">
<head>
<title>Home Page</title>
<meta content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
</head>
<body>
<img src="img_la.jpg" alt="LA" style="width:100%">
<div class="">
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</div>
</body>
</html>

H. U. Yakubu - CS Department, BUK. (Lecture 7) 7


Courses Page
<!DOCTYPE html>
<html lang="en">
<head>
<title>Courses</title>
<meta content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
</head>
<body>
<div class="">
<h1>Our Courses</h1>
<p>1. ITC3340</p>
<p>2. CSC3330</p>
</div>
</body>
</html>

H. U. Yakubu - CS Department, BUK. (Lecture 7) 8


Students page
<!DOCTYPE html>
<html lang="en">
<head>
<title>Students</title>
<meta content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
</head>
<body>
<div class="">
<h1>Our Students</h1>
<p>Software Engineering  150 students</p>
<p>Computer Science  200 students</p>
</div>
</body>
</html>

H. U. Yakubu - CS Department, BUK. (Lecture 7) 9


Base HTML File
 The content of the base.html file

<!DOCTYPE html>
<html lang="en">
<head>
<title>Student Info</title>
<meta content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="">
<script src=""></script>
</head>
<body>
{% block content %}
{% endblock content %}
</body>
</html>
H. U. Yakubu - CS Department, BUK. (Lecture 7) 10
Home Page (Updated)
 Edit the content of the home page to this:

{% extends ‘base.html’ %}

{% block content %}
<img src="img_la.jpg" alt="LA" style="width:100%">
<div class="">
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
</div>
{% endblock content %}
H. U. Yakubu - CS Department, BUK. (Lecture 7) 11
Courses Page (Updated)
Edit the content of the courses page to this:

{% extends ‘base.html’ %}

{% block content %}
<div class="">
<h1>Our Courses</h1>
<p>1. ITC3340</p>
<p>2. CSC3330</p>
</div>
{% endblock content %}

H. U. Yakubu - CS Department, BUK. (Lecture 7) 12


New Students Page (Updated)
Edit the content of the home page to this:

{% extends ‘base.html’ %}

{% block content %}
<div class="">
<h1>Our Students</h1>
<p>Software Engineering  150 students</p>
<p>Computer Science  200 students</p>
</div>
{% endblock content %}

H. U. Yakubu - CS Department, BUK. (Lecture 7) 13


Templates Tags

H. U. Yakubu - CS Department, BUK. (Lecture 7) 14


Tags
 Tags provide arbitrary logic in the rendering process.
 For example, a tag can output content, serve as a control
structure e.g. an “if” statement or a “for” loop, grab content
from a database, or even enable access to other template tags.
 Tags are surrounded by {% and %}
Example
{% csrf_token %}
This tag is used for CSRF (Cross Site Request Forgeries)
protection.

H. U. Yakubu - CS Department, BUK. (Lecture 7) 15


Extends
 This tag signals that this template extends a parent template.
This tag can be used in two ways:
o{% extends "base.html" %} (with quotes) uses the
literal value "base.html" as the name of the parent template
to extend.
o{% extends variable %} uses the value of variable. If the
variable evaluates to a string,
oDjango will use that string as the name of the parent
template. If the variable evaluates to a Template object,
Django will use that object as the parent template.

H. U. Yakubu - CS Department, BUK. (Lecture 7) 16


Tags Examples Contd.
 Defines a block that can be overridden by child templates
{% block %} {% endblock %}

H. U. Yakubu - CS Department, BUK. (Lecture 7) 17


Comments
 Comments in Django look like this:
o Single line comment
{# this won't be rendered #}

o Multi-line comment:
{% comment %}
This line, and
this line won’t be rendered.
They are all commented.
{% endcomment %}
H. U. Yakubu - CS Department, BUK. (Lecture 7) 18
If
 Some Tags require beginning and ending tags:
{% if male %}Dear Mr. {{ user.last_name }},
{% else %}
Dear Mrs.
{% endif %}

H. U. Yakubu - CS Department, BUK. (Lecture 7) 19


for
 Loops over each item in an array, making the item available in a
context variable. For example, to display a list of students
provided in all_students:
<ol>
{% for student in all_students %}
<li>
{{ student.last_name|upper }}, {{
student.first_name }}
</li>
{% endfor %}
</ol>
H. U. Yakubu - CS Department, BUK. (Lecture 7) 20
url
 The url tag is used as a value to the href attribute of an
anchor element for redirect.

{% url ‘name_of_a_path’ %}

Example
<a href = “{% url ‘name_of_a_path’
%}”>Students</a>

H. U. Yakubu - CS Department, BUK. (Lecture 7) 21


Filters
 Filters transform the values of variables and tag arguments.
They look like this:
 {{ student.last_name|upper }}
 {{ student.first_name|title }}
 {{ date_created|date }}

H. U. Yakubu - CS Department, BUK. (Lecture 7) 22


CRUD

H. U. Yakubu - CS Department, BUK. (Lecture 7) 23


CRUD Operations
 CRUD is an acronym that stands for CREATE, READ/RETRIEVE,
UPDATE, DELETE
 We have been carrying out these operations from the admin
site.
 Now, we shall use the client side for the same operations as
not all users should be allowed to access the admin site.

H. U. Yakubu - CS Department, BUK. (Lecture 7) 24


READ/RETRIEVE
 We shall begin with the READ/RETRIEVE operation being the
one of the simplest.
 add the all_courses view as:
From .models import Course

Def all_courses(request):
allcourses = Course.object.all()
context = {‘allcourses’: allcourses}
return render(request, ‘all-courses.html’, context)

H. U. Yakubu - CS Department, BUK. (Lecture 7) 25


READ/RETRIEVE Contd.
 Edit the all-courses.html file as:
{% for course in allcourses %}
{{ course.category }} <br>
{{ course.code }} <br>
{{ course.title }} <br>
{{ course.credit_unit }} <br>
{{ course.description }} <br><hr>
{% endfor %}
H. U. Yakubu - CS Department, BUK. (Lecture 7) 26
DELETE
 Create a new view called course_delete as in the views.py
file follows :
def course_delete(request, id):
course = Course.objects.get(id=id)
course.delete()
 Create the path for the above view in the urls.py file as
follows:
o Import the view
from .views import all_courses, course_delete

H. U. Yakubu - CS Department, BUK. (Lecture 7) 27


DELETE
 urlpatterns = [

path(‘<int:id>/delete/’, course_delete name=“course-delete”)
]

H. U. Yakubu - CS Department, BUK. (Lecture 7) 28

You might also like