21CS62
Program 1:Date and Time
Step 1:Terminal
django-admin startproject myproject
cd myproject
py manage.py startapp myapp
Step 2:settings.py
Open the settings.py file inside the myproject directory.
Find the INSTALLED_APPS list and add your app's name ('myapp') to the list.
INSTALLED_APPS = [
...
'myapp',
]
Step 3:views.py
Open the views.py file inside your myapp directory and
define a view.
from django.http import HttpResponse
from datetime import datetime, timedelta
def datetime_with_offsets(request):
now = datetime.now()
offset_hours = 4
four_hours_ahead = now + timedelta(hours=offset_hours)
four_hours_before = now - timedelta(hours=offset_hours)
html = f"<html><body><h1>Current Date and Time with Offsets:</h1>" \
f"<p>Current: {now}</p>" \
f"<p>Four Hours Ahead: {four_hours_ahead}</p>" \
f"<p>Four Hours Before: {four_hours_before}</p></body></html>"
return HttpResponse(html)
Step 4:myapp/urls.py (find/create this in myapp folder)
from django.urls import path
from . import views
urlpatterns = [
path('datetime_with_offsets/', views.datetime_with_offsets,
name='datetime_with_offsets'),
]
Step 5:myproject/urls.py (find this in myproject folder)
from django.contrib import admin
from django.urls import path, include # Import include
urlpatterns = [
path('admin/', admin.site.urls),
path('myapp/', include('myapp.urls')),
]
Step 6:Terminal (make sure you're in myproject directory else type 'cd myproject' in terminal)
py manage.py runserver
Step 7:Browser
go to http://127.0.0.1:8000/
http://127.0.0.1:8000/myapp/
http://127.0.0.1:8000/myapp/datetime_with_offsets/ for displaying output
***********************************************************************************
Program 2: Student and Fruit List
Step 1:Terminal
django-admin startproject event_manager
cd event_manager
py manage.py startapp events
Step 2:settings.py
Open the settings.py file inside the event_manager directory.
Find the INSTALLED_APPS list and add your app's name ('events') to the list.
INSTALLED_APPS = [
...
'events',
]
Step 3:events/models.py
from django.db import models
class Fruit(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Student(models.Model):
name = models.CharField(max_length=100)
event = models.CharField(max_length=100) # Assuming the event name is a string
selected = models.BooleanField(default=False)
def __str__(self):
return self.name
Step 4:events/admin.py
from django.contrib import admin
from .models import Fruit, Student
admin.site.register(Fruit)
admin.site.register(Student)
Step 5:views.py
Open the views.py file inside your events directory and
define a view.
from django.shortcuts import render
from .models import Fruit, Student
def fruit_list(request):
fruits = Fruit.objects.all()
return render(request, 'events/fruit_list.html', {'fruits': fruits})
def student_list(request):
students = Student.objects.filter(selected=True)
return render(request, 'events/student_list.html', {'students': students})
Step 6:events/templates/events (create folder 'templates' in events directory and then another new 'events' folder within templates)
Create two templates fruit_list.html and student_list.html files here
fruit_list.html:
<!DOCTYPE html>
<html>
<head>
<title>Fruit List</title>
</head>
<body>
<h1>Available Fruits:</h1>
<ul>
{% for fruit in fruits %}
<li>{{ fruit.name }}</li>
{% endfor %}
</ul>
</body>
</html>
student_list.html:
<!DOCTYPE html>
<html>
<head>
<title>Selected Students</title>
</head>
<body>
<h1>Selected Students for the Event:</h1>
<ol>
{% for student in students %}
<li>{{ student.name }}</li>
{% endfor %}
</ol>
</body>
</html>
Step 7:events/urls.py (find/create this in events folder)
from django.urls import path
from . import views
urlpatterns = [
path('fruits/', views.fruit_list, name='fruit_list'),
path('students/', views.student_list, name='student_list'),
]
Step 8:event_manager/urls.py (find this in event_manager folder)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('events/', include('events.urls')),
]
Step 10:Terminal
python manage.py check
python manage.py makemigrations
python manage.py migrate
python manage.py shell
>>> from events.models import Fruit
>>> p1=Fruit(name='Orange')
>>> p1.save()
>>> p2=Fruit(name='Apple')
>>> p2.save()
>>> p=Fruit.objects.all()
>>> p
<QuerySet [<Fruit: Orange>,<Fruit: Apple>]>
>>> from events.models import Student
>>> q1=Student(name='John',event='Dance',selected=True)
>>> q1.save()
>>> q2=Student(name='Sam',event='Dance',selected=True)
>>> q2.save()
>>> q=Student.objects.all()
>>> q
<QuerySet [<Student: John>,<Student: Sam>]>
>>>exit()
Step 11:Terminal (make sure you're in event_manager directory else type 'cd event_manager' in terminal)
py manage.py runserver
Step 12:Browser
go to http://127.0.0.1:8000/
http://127.0.0.1:8000/events/
http://127.0.0.1:8000/events/fruits/ and http://127.0.0.1:8000/events/students/ for displaying output
***********************************************************************************
Program 3: Course Registration
Step 1:Terminal
django-admin startproject course_registration
cd course_registration
py manage.py startapp registration
Step 2:settings.py
Open the settings.py file inside the event_manager directory.
Find the INSTALLED_APPS list and add your app's name ('events') to the list.
INSTALLED_APPS = [
...
'registration',
]
Step 3: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
Step 4:registration/admin.py
from django.contrib import admin
from .models import Course, Student
admin.site.register(Course)
admin.site.register(Student)
Step 5:registration/views.py
Open the views.py file inside your registration directory and
define a view.
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})
Step 6:registration/templates/registration (create folder 'templates' in registration directory and then another new 'registration' folder within templates)
Create three templates course_list.html, student_registration.html and registration_confirmation.html files here
course_list.html:
<!DOCTYPE html>
<html>
<head>
<title>Course List</title>
</head>
<body>
<h1>Available Courses:</h1>
<ol>
{% for c in courses %}
<li>{{ c.name }}</li>
{% endfor %}
</ol>
</body>
</html>
student_registration.html:
<!DOCTYPE html>
<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" id="name" name="name" value="">
<label for="email">Email: </label>
<input type="text" id="email" name="email" value="">
<input type="submit" value="Submit">
</form>
</body>
</html>
registration_confirmation.html:
<!DOCTYPE html>
<html>
<head>
<title>Confirmation Page</title>
</head>
<body>
<h1>Confirmed!!!</h1>
<p>{{ message }}</p>
</body>
</html>
Step 7:registration/urls.py (find/create this in events folder)
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'),
]
Step 8:course_registration/urls.py (find this in event_manager folder)
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('registration.urls')),
]
Step 10:Terminal
python manage.py check
python manage.py makemigrations
python manage.py migrate
python manage.py shell
>>> from registration.models import Course
>>> p1=Course(name='FSD',description='To create websites')
>>> p1.save()
>>> Course.objects.all()
<QuerySet [<Course: FSD>]>
>>>exit()
Step 11:Terminal (make sure you're in course_registration directory else type 'cd course_registration' in terminal)
py manage.py runserver
Step 12:Browser
go to http://127.0.0.1:8000/ to view courses
http://127.0.0.1:8000/register/1/ to register