Django (Day 5)
Update Templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{% for student in students %}
<a href="{% url 'student' student.id %}">{{student.name}}</a><br>
{% endfor %}
<form action="{% url 'createStudent' %}" method="POST">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit">
</form>
</body>
</html>
Update Templates/Student.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{{student.name}}<br>
{{student.age}}<br>
{{student.branch}}<br>
{{student.marks}}<br>
{{student.college}}<br>
{% if students.marks >= 90 %}
<h1>PASS</h1>
{% else %}
<h1>FAIL</h1>
{% endif %}
</body>
</html>
Update test_app/models.py
from django.db import models
from django.forms import ModelForm
# Create your models here.
class Student(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
branch = models.CharField(max_length=100)
marks = models.IntegerField()
college = models.CharField(max_length=100)
def __str__(self):
return f"{self.name}"
class StudentForm(ModelForm):
class Meta:
model = Student
fields = ['name', 'age', 'branch', 'marks', 'college']
Update test_app/urls.py
from django.urls import path
from test_app import views
urlpatterns = [
path('student', views.index, name='students'),
path('student/<int:pk>/', views.get_student, name='student'),
path('createStudent', views.createStudent, name='createStudent'),
]
Update test_app/views.py
from django.http import HttpResponse
from django.shortcuts import render
from .models import Student, StudentForm
def index(request):
std=Student.objects.all()
return render(request,'index.html',{'students':std, "form":StudentForm()})
def get_student(request,pk):
std=Student.objects.get(id=pk)
return render(request,'Student.html',{'student':std})
def createStudent(request):
if request.method=="POST":
student=StudentForm(request.POST)
if student.is_valid():
student.save()
return render(request,'index.html',{'students':Student.objects.all()})
Note: Any Doubts Contact Hemanth Sir
Comments
Post a Comment