0% found this document useful (0 votes)
7 views15 pages

FSD Programs

Hbvvfggv

Uploaded by

iamjohnmohd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views15 pages

FSD Programs

Hbvvfggv

Uploaded by

iamjohnmohd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

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 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>

python manage.py runserver

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 (website_pages /urls.py)


from django.urls import path
from website_pages import views
urlpatterns = [
path('home/', views.home, name='home'),
path('about_us/', views.about_us, name='about_us'),
path('contact_us/', views.contact_us, name='contact_us'),
]

#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’,

#templates/layout.html (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>&copy; 2024 My Website. All rights reserved. </p>

<p>Developed by CIT</p>

</footer>

</body>

</html>

#home.html (website_pages/templates/website_pages/home.html)

{% extends 'layout.html' %}

{% 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 'layout.html' %}

{% 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 'layout.html' %}

{% 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@cambridge.edu.in</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 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.

django-admin startproject course_registration

cd course_registration

py manage.py startapp registration

registration/models.py

from django.db import models

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()

courses = models.ManyToManyField(Course, related_name='students')

def __str__(self):

return self.name

registration/admin.py

from django.contrib import admin


from .models import Course, Student

admin.site.register(Course)

admin.site.register(Student)

py manage.py check

py manage.py makemigrations

py manage.py migrate

py manage.py shell

>>> from registration.models import Course

>>> p1=Course(name='FSD',description='to create the website')

>>> p1.save()

>>> Course.objects.all()

registration/views.py

from django.shortcuts import render

from .models import Course

def course_list(request):

courses = Course.objects.all()

return render(request, 'registration/course_list.html', {'courses': courses})

def register_student(request, course_id):

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:

message = f'{student.name} registered successfully for {course.name}.'

else:

message = f'{student.name} is already registered for {course.name}.'

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>{{ c.name }}</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/urls.py

from django.urls import path

from . import views

urlpatterns = [

path('', views.course_list, name='course_list'),

path('register/<int:course_id>/', views.register_student, name='register_student'),

urls.py

from django.contrib import admin

from django.urls import path, include

urlpatterns = [

path('admin/', admin.site.urls),

path('', include('registration.urls')),

python manage.py runserver

You might also like