База данных в PostrgreSQL(PgAdmin).
Мой вариант кода, вообще ничего не выбает!
urls.py
from django.contrib import admin
from django.urls import path
from mainapp import views
urlpatterns = [
path("", views.index),
]
models.py
from django.db import models
# Create your models here.
class GradeValues(models.Model):
QGrade = models.TextField()
class stuclasses(models.Model):
StudentID = models.IntegerField()
Grade = models.ForeignKey(GradeValues, related_name='GrateValues_Grade', on_delete=models.CASCADE)
class courses(models.Model):
CourseName = models.TextField()
CourseDesc = models.TextField()
Credits = models.IntegerField()
DeptID = models.TextField()
class classes(models.Model):
CourseID = models.ForeignKey(courses, related_name='courses_CourseID', on_delete=models.CASCADE)
Term = models.TextField()
Year = models.IntegerField()
Instructor = models.TextField()
Classroom = models.TextField()
ClassTime = models.TextField()
class stuclasses_classes(models.Model):
stuclasses_ClassID = models.ForeignKey(stuclasses, related_name='stuclasses_ClassID', on_delete=models.CASCADE)
classes_ClassID = models.ForeignKey(classes, related_name='classes_ClassID', on_delete=models.CASCADE)
class students(models.Model):
LastName = models.TextField()
FirstName = models.TextField()
MiddleName = models.TextField()
Addr1 = models.TextField()
Addr2 = models.TextField()
City = models.TextField()
StateProvince = models.TextField()
Country = models.TextField()
PostalCode = models.TextField()
Email = models.TextField()
class stuclasses_students(models.Model):
stuclasses_StudentID = models.ForeignKey(stuclasses, related_name='stuclasses_StudentID', on_delete=models.CASCADE)
students_StudentID = models.ForeignKey(students, related_name='students_StudentID', on_delete=models.CASCADE)
class Person(models.Model):
name = models.CharField(max_length=20)
age = models.IntegerField()
views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponseNotFound
from .models import students
# получение данных из бд
def index(request):
people = students.objects.all()
return render(request, "index.html", {"people": people})
from .models import courses
def index(request):
course = courses.objects.all()
return render(request, "index.html", {"course": course})
from .models import GradeValues
def index(request):
grade = GradeValues.objects.all()
return render(request, "index.html", {"grade": grade})
from .models import classes
def index(request):
class = classes.objects.all()
return render(request, "index.html", {"class": class})
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>METANIT.COM</title>
</head>
<body>
{% csrf_token %}
{% if people.count > 0 %}
<table>
<thead>
<th>Id</th>
<th>Имя</th>
<th>Фамилия</th>
<th></th>
</thead>
{% for person in people %}
<tr>
<td>{{ person.id }}</td>
<td>{{ person.LastName }}</td>
<td>{{ person.FirstName }}</td>
<td>{{ course.CourseName }}</td>
<td>{{ grade.Grade }}</td>
<td>{{ class.Year }}</td>
<td>{{ class.Term }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
</body>
</html>