0% found this document useful (0 votes)
2 views6 pages

Assignment 5 Q2

The document outlines the creation of a Django app with two models, Student and Result, which are connected via a ManyToMany field. It provides instructions for setting up the database, creating a superuser, and implementing views and templates to display student results using AJAX and jQuery. Additionally, it includes code snippets for models, views, and HTML templates necessary for the app's functionality.

Uploaded by

Shweta Nirmanik
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)
2 views6 pages

Assignment 5 Q2

The document outlines the creation of a Django app with two models, Student and Result, which are connected via a ManyToMany field. It provides instructions for setting up the database, creating a superuser, and implementing views and templates to display student results using AJAX and jQuery. Additionally, it includes code snippets for models, views, and HTML templates necessary for the app's functionality.

Uploaded by

Shweta Nirmanik
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/ 6

Assignment 5

Q2. Create a database Results. Develop a Django app that has 2 models:
Student(usn, name, sem, branch)
Result(subject_code, subject_name, cie_marks, see_marks)
and a ManyToMany field student.

Enter the data to both models directly with phpMyAdmin of DjangoAdmin Interfaces.

Develop a Django app that has a template html file to collect usn and a submit
button. On clicking submit button, corresponding student results should be
displayed in tabular form in the same page using AJAX and Jquery.

As a bonus, try to display the name of the student as well.

Solution
1. In assign4 folder
Admin.py
from .models import Student, Result

admin.site.register(Student)
admin.site.register(Result)

2. In terminal RUN THE COMMANDS


python manage.py makemigrations assign4
python manage.py migrate
python manage.py createsuperuser
Username (leave blank to use 'shwet'): day4
Email address: [email protected]
Password: day5
Password (again): day5
The password is too similar to the username.
This password is too short. It must contain at least 8 characters.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.
PS D:\fdp>python manage.py runserver

3. Models.py
class Student(models.Model):
usn = models.CharField(max_length=20)
name = models.CharField(max_length=100)
sem = models.IntegerField()
branch = models.CharField(max_length=100)
def __str__(self):
return self.name

class Result(models.Model):
subject_code = models.CharField(max_length=20)
subject_name = models.CharField(max_length=100)
cie_marks = models.FloatField()
see_marks = models.FloatField()
students = models.ManyToManyField(Student)
def __str__(self):
return self.subject_name
4. Views.py
def index(request):
return render(request, 'display_results.html')

def get_results(request):
if request.method == 'POST':
usn = request.POST.get('usn')
try:
student = Student.objects.get(usn=usn)
results = student.result_set.all()
data = []
for result in results:
data.append({
'subject_name': result.subject_name,
'cie_marks': result.cie_marks,
'see_marks': result.see_marks
})
return HttpResponse({'results': data, 'name': student.name})
except Student.DoesNotExist:
return HttpResponse({'error': 'Student with provided USN does not exist.'})
return HttpResponse({'error': 'Invalid request method.'})

5. Display_results.html
{% load static %}
<html>
<head>
<script src="{% static 'jquery.min.js' %}"> </script>
</head>
<body>
<h1>Student Results</h1>
<form id="usnForm">
<label for="usn">Enter USN:</label>
<input type="text" id="usn" name="usn">
<button type="submit" id="submitBtn">Submit</button>
</form>
<div id="resultsTable"></div>
<script>
$(document).ready(function() {
$('#usnForm').submit(function(e) {
e.preventDefault();
var usn = $('#usn').val();
$.ajax({
type: 'POST',
url: '/get_results/',
data: {
'usn': usn,
'csrfmiddlewaretoken': '{{ csrf_token }}'
},
dataType: 'json',
success: function(data) {
if ('error' in data) {
$('#resultsTable').html('<p>' + data.error + '</p>');
} else {
var table = '<h2>Results for ' + data.name + '</h2>';
table += '<table border="1"><tr><th>Subject Name</th><th>CIE
Marks</th><th>SEE Marks</th></tr>';
$.each(data.results, function(index, result) {
table += '<tr><td>' + result.subject_name + '</td><td>' +
result.cie_marks + '</td><td>' + result.see_marks + '</td></tr>';
});
table += '</table>';
$('#resultsTable').html(table);
}
}
});
});
});
</script>
</body>
</html>

6. Output

You might also like