Program-3
Develop a Django app that displays current date and time in server.
Terminal:
py--version
pip--version
py -m pip install Django
django-admin startproject mysite
cd mysite
py [Link] runserver
[Link]/mysite
from [Link] import HttpResponse
import datetime
def current_datetime(request):
now=[Link]()
html=”<html><body>It is now %s.</body></html>”%now
return HttpResponse(html)
[Link]/mysite
from [Link] import path
from [Link] import current_datetime
urlpatterns=[
path(‘’,current_datetime),
]
Program-4
Develop a Django app that displays date and time four hours ahead and four hours before as
an offset of current date and time in server.
Terminal:
py [Link] startapp mysite1
[Link]/mysite1
from [Link] import HttpResponse
import datetime
def current_datetime(request):
now=[Link]()
html=”<html><body>It is now %s.</body></html>”%now
return HttpResponse(html)
def four_hours_ahead(request):
dt=[Link]()+[Link](hours=4)
html=”<html><body>After 4 hour(s), it will be %s.</body></html>”%(dt,)
return HttpResponse(html)
def four_hours_before(request):
dt=[Link]()+[Link](hours=-4)
html=”<html><body>Before 4 hour(s), it will be %s.</body></html>”%(dt,)
return HttpResponse(html)
[Link]/mysite1
from [Link] import path
from [Link] import current_datetime,four_hours_ahead,four_hours_before
urlpatterns=[
path(‘’,current_datetime),
path(‘fhrsa/’, four_hours_ahead),
path(‘fhrsb/’, four_hours_before),
]
Program-5
Develop a simple Django app that displays an unordered list of fruits and ordered list of
selected students for an event.
Terminal:
python [Link] startapp fruits_and_students
[Link]/fruits_and_students
from [Link] import render
def fruits_and_students(request):
print(request.build_absolute_uri())
fruits = ['Apple', 'Banana', 'Orange', 'Grapes']
students = ['Alice', 'Bob', 'Charlie', 'David']
return render(request, 'fruits_and_students/fruits_and_students.html', {'fruits': fruits, 'students':
students})
[Link] (fruits_and_students/[Link])
from [Link] import path
from .views import fruits_and_students
urlpatterns = [
path('', fruits_and_students, name='fruits_and_students'),
]
[Link] (myproject/[Link])
from [Link] import admin
from [Link] import path, include
urlpatterns = [
path("", include("[Link]")),
path("admin/", [Link]),
path('fruits_and_students/', include('fruits_and_students.urls')),
]
myproject/[Link]/ INSTALLED_APPS
fruits_and_students’,
fruits_and_students.html(fruits_and_students/templates/fruits_and_students/fruits_and_st
[Link])
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fruits and Students</title>
</head>
<body>
<h1>Fruits</h1>
<ul>
{% for fruit in fruits %}
<li>{{ fruit }}</li>
{% endfor %}
</ul>
<h1>Selected Students</h1>
<ol>
{% for student in students %}
<li>{{ student }}</li>
{% endfor %}
</ol>
</body>
</html>
python [Link] runserver
Program-6
Develop a [Link] with a suitable header (containing navigation menu) and footer with
copyright and developer information. Inherit this [Link] and create 3 additional pages:
contact us, About Us and Home page of any website.
Terminal:
python [Link] startapp website_pages
website_pages/[Link]
#[Link]
from [Link] import render
def home(request):
return render(request, 'website_pages/[Link]')
def about_us(request):
return render(request, 'website_pages/about_us.html')
def contact_us(request):
return render(request, 'website_pages/contact_us.html')
[Link] (website_pages /[Link])
from [Link] import path
from website_pages import views
urlpatterns = [
path('home/', [Link], name='home'),
path('about_us/', views.about_us, name='about_us'),
path('contact_us/', views.contact_us, name='contact_us'),
]
#[Link] (myproject/[Link])
from [Link] import admin
from [Link] import path, include
urlpatterns = [
path("", include("[Link]")),
path("admin/", [Link]),
path('fruits_and_students/', include('fruits_and_students.urls')),
path('', include('website_pages.urls')),
]
myproject/[Link]/ INSTALLED_APPS
‘website_pages’,
#templates/[Link] (The CSS is optional)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{% block title %}My Website{% endblock %}</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
header {
background-color: #333;
color: #fff;
padding: 10px;
nav ul {
list-style-type: none;
padding: 0;
nav ul li {
display: inline;
margin-right: 20px;
nav ul li a {
text-decoration: none;
color: #fff;
main {
padding: 20px;
footer {
background-color: #333;
color: #fff;
text-align: center;
padding: 10px;
position: fixed;
bottom: 0;
width: 100%;
</style>
</head>
<body>
<header>
<nav>
<ul>
<li><a href="{% url 'home' %}">Home</a></li>
<li><a href="{% url 'about_us' %}">About Us</a></li>
<li><a href="{% url 'contact_us' %}">Contact Us</a></li>
</ul>
</nav>
</header>
<main>
{% block content %}
{% endblock %}
</main>
<footer>
<p>© 2024 My Website. All rights reserved. </p>
<p>Developed by CIT</p>
</footer>
</body>
</html>
#[Link] (website_pages/templates/website_pages/[Link])
{% extends '[Link]' %}
{% block title %}Home{% endblock %}
{% block content %}
<h1>Welcome to Cambridge Institute of Technology</h1>
<p>Empowering students with a blend of knowledge and innovation.</p>
<p>Nestled in the bustling city of Bengaluru, our campus is a hub of academic excellence
and
cutting-edge research.</p>
<h2>Discover Your Potential</h2>
<ul>
<li><strong>Undergraduate Programs:</strong> Dive into our diverse range of engineering
courses designed to fuel your passion and drive innovation.</li>
<li><strong>Postgraduate Programs:</strong> Advance your expertise with our specialized
master's programs and embrace leadership in technology.</li>
</ul>
<p>Join our vibrant community where ideas flourish and inventions come to life in our state-
of-theart labs and research centers.</p>
<p>Benefit from our strong industry ties and placement programs that open doors to exciting
career
opportunities.</p>
{% endblock %}
#about_us.html (website_pages/templates/website_pages/about_us.html)
{% extends '[Link]' %}
{% block title %}About Us{% endblock %}
{% block content %}
<h1>Our Legacy</h1>
<p>Founded on the principles of quality education and societal contribution, we've been at
the
forefront of technological education for over four decades.</p>
<h1>Vision and Mission</h1>
<p>Our vision is to be a beacon of knowledge that lights the way for aspiring minds, and our
mission
is to nurture innovative thinkers who will shape the future of technology.</p>
<h1>Campus Life</h1>
<p>Experience a dynamic campus life enriched with cultural activities, technical clubs, and
community service initiatives that foster holistic development.</p>
{% endblock %}
#contact_us.html (website_pages/templates/website_pages/contact_us.html)
{% extends '[Link]' %}
{% block title %}Contact Us{% endblock %}
{% block content %}
<h1>Get in Touch</h1>
<p>For admissions and inquiries, reach out to us at:</p>
<ul>
<li><strong>Email:</strong> admissions@[Link]</li>
<li><strong>Phone:</strong> +91-9731998888</li>
</ul>
<h1>Visit Our Campus</h1>
<p>Cambridge Institute of Technology - Main Campus</p>
<p>KR Puram, Bengaluru - 560036</p>
<p>We welcome you to be a part of our thriving community that's dedicated to creating a
better
tomorrow through technology and innovation.</p>
{% endblock %}
python [Link] runserver
Program-7
Develop a Django app that performs student registration to a course. It should also
display list of students registered for any selected course. Create students and course as
models with enrolment as ManyToMany field.
django-admin startproject course_registration
cd course_registration
py [Link] startapp registration
registration/[Link]
from [Link] import models
class Course([Link]):
name = [Link](max_length=100)
description = [Link]()
def __str__(self):
return [Link]
class Student([Link]):
name = [Link](max_length=100)
email = [Link]()
courses = [Link](Course, related_name='students')
def __str__(self):
return [Link]
registration/[Link]
from [Link] import admin
from .models import Course, Student
[Link](Course)
[Link](Student)
py [Link] check
py [Link] makemigrations
py [Link] migrate
py [Link] shell
>>> from [Link] import Course
>>> p1=Course(name='FSD',description='to create the website')
>>> [Link]()
>>> [Link]()
registration/[Link]
from [Link] import render
from .models import Course
def course_list(request):
courses = [Link]()
return render(request, 'registration/course_list.html', {'courses': courses})
def register_student(request, course_id):
course = [Link](pk=course_id)
if [Link] == 'POST':
name = [Link]('name')
email = [Link]('email')
student, created = [Link].get_or_create(name=name, email=email)
if created:
message = f'{[Link]} registered successfully for {[Link]}.'
else:
message = f'{[Link]} is already registered for {[Link]}.'
return render(request, 'registration/registration_confirmation.html', {'message':
message})
return render(request, 'registration/student_registration.html', {'course': course})
course_list.html
<!DOCTYPE html>
<html>
<head>
<title>course List</title>
</head>
<body>
<h1>Available courses:</h1>
<ul>
{% for c in courses %}
<li>{{ [Link] }}</li>
{% endfor %}
</ul>
</body>
</html>
student_registration.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Course Registration</title>
</head>
<body>
<h1>Course Registration</h1>
<form action="." method="POST">
{% csrf_token %}
<label for="name">Name: </label>
<input type="text" name="name" value="">
<label for="email">Email: </label>
<input type="text" name="email" value="">
<input type="submit" value="Submit">
</form>
</body>
</html>
registration_confirmation.html
<!DOCTYPE html>
<html>
<head>
<title>course List</title>
</head>
<body>
<h1>Available courses:</h1>
<ul>
<li>{{ message }}</li>
</ul>
</body>
</html>
registration/[Link]
from [Link] import path
from . import views
urlpatterns = [
path('', views.course_list, name='course_list'),
path('register/<int:course_id>/', views.register_student, name='register_student'),
[Link]
from [Link] import admin
from [Link] import path, include
urlpatterns = [
path('admin/', [Link]),
path('', include('[Link]')),
python [Link] runserver