[go: up one dir, main page]

0% found this document useful (0 votes)
4 views5 pages

FSD 12 13

The document outlines two Django web applications: one for student registration and another for searching student courses. The registration app includes a form that submits student data via AJAX, while the search app allows users to find courses associated with a student by name. Both applications utilize models for Course and Student, and include necessary views, URLs, and scripts for functionality.
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)
4 views5 pages

FSD 12 13

The document outlines two Django web applications: one for student registration and another for searching student courses. The registration app includes a form that submits student data via AJAX, while the search app allows users to find courses associated with a student by name. Both applications utilize models for Course and Student, and include necessary views, URLs, and scripts for functionality.
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/ 5

Program 12:

templates/registration.html
<!DOCTYPE html>
<html>
<head>
<title>Student Registration</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1>Student Registration</h1>
<form id="registration-form" method="post">
{% csrf_token %}
<label for="name">Name:</label>
<input type="text" id="name" name="name" required /><br />
<label for="email">Email:</label>
<input type="email" id="email" name="email" required /><br />
<label for="course">Course:</label>
<select id="course" name="course" required>
{% for course in courses %}
<option value="{{ course.id }}">{{ course.name }}</option>
{% endfor %}
</select><br />
<button type="submit">Register</button>
</form>
<div id="message"></div>
<script>
$(document).ready(function () {
$("#registration-form").submit(function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: '{% url "register_student" %}',
data: $(this).serialize(),
success: function (response) {
if (response.success) {
$("#message").text("Registration successful!");
$("#registration-form")[0].reset();
} else {
$("#message").text("Error: Registration failed.");
}
},
});
});
});
</script>
</body>
</html>
Admin.py
from django.contrib import admin

# Register your models here.


from .models import Course

admin.site.register(Course)

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()
course = models.ForeignKey(Course, on_delete=models.CASCADE)
def str (self):
return self.name

views.py
from django.shortcuts import render

# Create your views here.


from django.shortcuts import render
from django.http import JsonResponse
from .models import Course, Student
def registration_page(request):
courses = Course.objects.all()
return render(request, 'registration.html', {'courses': courses})

def register_student(request):
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
course_id = request.POST.get('course')
course = Course.objects.get(pk=course_id)
student = Student.objects.create(name=name, email=email, course=course)
return JsonResponse({'success': True})
return JsonResponse({'success': False})

def register_student(request):
if request.method == 'POST':
name = request.POST.get('name')
email = request.POST.get('email')
course_id = request.POST.get('course')
course = Course.objects.get(pk=course_id)
student = Student.objects.create(name=name, email=email, course=course)

elif request.method == 'GET':


students = Student.objects.all().values('name', 'email', 'course__name')
student_list = list(students)
return JsonResponse({'students': student_list})

return JsonResponse({'success': False, 'error': 'Invalid course ID'})

urls.py
from django.urls import path
from .views import register_student, registration_page
urlpatterns = [
path('', registration_page, name='registration_page'),
path('register/',register_student, name='register_student'),
]

Scripts to run the program


python manage.py makemigrations
python manage.py migrate
python manage.py createsueruser
python manage.py runserver

Program 13

templates/search_student.html
<!DOCTYPE html>
<html>
<head>
<title>Search Student Courses</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
$('#search').on('click', function() {
var student_name = $('#student_name').val();
$.ajax({
url: '{% url "get_student_courses" %}',
data: {
'student_name': student_name
},
dataType: 'json',
success: function(data) {
$('#results').empty();
if (data.courses.length > 0) {
$.each(data.courses, function(index, course) {
$('#results').append('<li>' + course.name + '</li>');
});
} else {
$('#results').append('<li>No courses found</li>');
}
}
});
});
});
</script>
</head>
<body>
<h1>Search for Student Courses</h1>
<input type="text" id="student_name" placeholder="Enter student name">
<button id="search">Search</button>
<ul id="results"></ul>
</body>
</html>

admin.py

models.py
from django.db import models

class Course(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
return self.name

class Student(models.Model):
name = models.CharField(max_length=100)
courses = models.ManyToManyField(Course)

def __str__(self):
return self.name

views.py
from django.shortcuts import render
from django.http import JsonResponse
from .models import Student

def search_student(request):
return render(request, 'search_student.html')

def get_student_courses(request):
student_name = request.GET.get('student_name')
student = Student.objects.filter(name__icontains=student_name).first()
courses = []
if student:
courses = list(student.courses.values('name'))
return JsonResponse({'courses': courses})

urls.py
from django.urls import path
from .views import search_student, get_student_courses

urlpatterns = [
path('', search_student, name='search_student'),
path('get_student_courses/', get_student_courses, name='get_student_courses'),
]

Scripts to run the program


python manage.py makemigrations
python manage.py migrate
python manage.py createsueruser
python manage.py runserver

You might also like