FSD Programs
FSD Programs
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 manage.py runserver
Views.py/mysite
from django.http import HttpResponse
import datetime
def current_datetime(request):
now=datetime.datetime.now()
html=”<html><body>It is now %s.</body></html>”%now
return HttpResponse(html)
urls.py/mysite
from django.urls import path
from mysite.views 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 manage.py startapp mysite1
Views.py/mysite1
from django.http import HttpResponse
import datetime
def current_datetime(request):
now=datetime.datetime.now()
html=”<html><body>It is now %s.</body></html>”%now
return HttpResponse(html)
def four_hours_ahead(request):
dt=datetime.datetime.now()+datetime.timedelta(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=datetime.datetime.now()+datetime.timedelta(hours=-4)
html=”<html><body>Before 4 hour(s), it will be %s.</body></html>”%(dt,)
return HttpResponse(html)
urls.py/mysite1
from django.urls import path
from mysite.views 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 manage.py startapp fruits_and_students
views.py/fruits_and_students
from django.shortcuts 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})
urls.py (fruits_and_students/urls.py)
from django.urls import path
from .views import fruits_and_students
urlpatterns = [
path('', fruits_and_students, name='fruits_and_students'),
]
urls.py (myproject/urls.py)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("", include("myapp.urls")),
path("admin/", admin.site.urls),
path('fruits_and_students/', include('fruits_and_students.urls')),
]
myproject/settings.py/ INSTALLED_APPS
fruits_and_students’,
fruits_and_students.html(fruits_and_students/templates/fruits_and_students/fruits_and_st
udents.html)
<!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>
Program-6
Develop a layout.html with a suitable header (containing navigation menu) and footer with
copyright and developer information. Inherit this layout.html and create 3 additional pages:
contact us, About Us and Home page of any website.
Terminal:
python manage.py startapp website_pages
website_pages/views.py
#views.py
from django.shortcuts import render
def home(request):
return render(request, 'website_pages/home.html')
def about_us(request):
return render(request, 'website_pages/about_us.html')
def contact_us(request):
return render(request, 'website_pages/contact_us.html')
#urls.py (myproject/urls.py)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("", include("myapp.urls")),
path("admin/", admin.site.urls),
path('fruits_and_students/', include('fruits_and_students.urls')),
path('', include('website_pages.urls')),
]
myproject/settings.py/ INSTALLED_APPS
‘website_pages’,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body {
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>
</ul>
</nav>
</header>
<main>
{% block content %}
{% endblock %}
</main>
<footer>
<p>Developed by CIT</p>
</footer>
</body>
</html>
#home.html (website_pages/templates/website_pages/home.html)
{% extends 'layout.html' %}
{% block content %}
<p>Nestled in the bustling city of Bengaluru, our campus is a hub of academic excellence
and
cutting-edge research.</p>
<ul>
</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 'layout.html' %}
{% block content %}
<h1>Our Legacy</h1>
<p>Founded on the principles of quality education and societal contribution, we've been at
the
<h1>Campus Life</h1>
<p>Experience a dynamic campus life enriched with cultural activities, technical clubs, and
{% endblock %}
#contact_us.html (website_pages/templates/website_pages/contact_us.html)
{% extends 'layout.html' %}
{% block content %}
<h1>Get in Touch</h1>
<ul>
<li><strong>Email:</strong> admissions@cambridge.edu.in</li>
<li><strong>Phone:</strong> +91-9731998888</li>
</ul>
<p>We welcome you to be a part of our thriving community that's dedicated to creating a
better
{% endblock %}
python manage.py 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.
cd course_registration
registration/models.py
class Course(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
def __str__(self):
return self.name
class Student(models.Model):
name = models.CharField(max_length=100)
email = models.EmailField()
def __str__(self):
return self.name
registration/admin.py
admin.site.register(Course)
admin.site.register(Student)
py manage.py check
py manage.py makemigrations
py manage.py migrate
py manage.py shell
>>> p1.save()
>>> Course.objects.all()
registration/views.py
def course_list(request):
courses = Course.objects.all()
course = Course.objects.get(pk=course_id)
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
student, created = course.students.get_or_create(name=name, email=email)
if created:
else:
course_list.html
<!DOCTYPE html>
<html>
<head>
<title>course List</title>
</head>
<body>
<h1>Available courses:</h1>
<ul>
{% for c in courses %}
{% 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>
{% csrf_token %}
</form>
</body>
</html>
registration_confirmation.html
<!DOCTYPE html>
<html>
<head>
<title>course List</title>
</head>
<body>
<h1>Available courses:</h1>
<ul>
</ul>
</body>
</html>
registration/urls.py
urlpatterns = [
urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('registration.urls')),