Django learning day 3 (01)
content
- Creation of class table and student table
- Association between class table and student table
- Student-class-corresponding associations
Complete the following work:
create a class table, student table
1.url—> have a class page, display class information—> jump—>
2. Display all student information pages
Creation of class table and student table
1. Create a new grade project, which is not detailed here, please refer to the previous content
2. Define the all_grade method and all_stu method
def all_grade(request):
grades = Grade.objects.all()
return render(request, 'grades.html', {'grades': grades})
def all_stu(request):
students = Student.objects.all()
return render(request, 'students.html', {'students': students})
3. Configure the url route
main route
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^stu/', include('stu.urls')),
url(r'^grade/', include('grade.urls')),
]
grade routing
from django.conf.urls import url
from grade import views
urlpatterns = [
url(r'^all_grade/', views.all_grade)
]
stu routing
from django.conf.urls import url
from stu import views
urlpatterns = [
url(r'^all_stu/', views.all_stu),
]
4. Create templates files (ie html files) for students and grades
grades file - class list
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>班级列表</title>
</head>
<body>
{% for grade in grades %}
班级ID:{{ grade.id }}
班级名称:{{ grade.g_name }}
<br>
{% endfor %}
</body>
</html>
students file—students page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生页面</title>
</head>
<body>
{% for student in students %}
学生ID: {{ student.id }}
学生姓名:{{ student.s_name }}
学生年龄:{{ student.s_age }}
班级ID:{{ student.g_id }}
<br>
{% endfor %}
</body>
</html>
Association between class table and student table
1. Click on the class table to jump to the student page under the class
# 获取所有学生信息
def all_stu(request):
students = Student.objects.all()
return render(request, 'students.html', {'students': students})
Corresponding html file content
{% for grade in grades %}
班级ID:{{ grade.id }}
# 这里增加了一个超链接,从班级表链接到学生表
<a href="/stu/all_stu/">
班级名称:{{ grade.g_name }}
</a>
<br>
{% endfor %}
2. Click on the class table to jump to the student page under the class (specific class id)
# 通过班级id,获取具体学生信息
def all_stu(request):
students = Student.objects.filter(g_id=3)
return render(request, 'students.html', {'students': students})
{% for grade in grades %}
班级ID:{{ grade.id }}
# 这里增加了一个超链接,从班级表链接到学生表
<a href="/stu/all_stu/">
班级名称:{{ grade.g_name }}
</a>
<br>
{% endfor %}
Student-class-corresponding associations
1. The id of the class to be added in the student's information corresponds to it
# ?g_id={{ grade.id }}
<body>
{% for grade in grades %}
班级ID:{{ grade.id }}
<a href="/stu/all_stu/?g_id={{ grade.id }}">
班级名称:{{ grade.g_name }}
</a>
<br>
{% endfor %}
</body>
2. Get the class information on the student's page, and return all the information whose query results do not meet the conditions
def all_stu(request):
#用GET.get来接收班级表的信息
g_id = request.GET.get('g_id')
students = Student.objects.filter(g_id=g_id)
return render(request, 'students.html', {'students': students})
Supporting the use of
{% for grade in grades %}
班级ID:{{ grade.id }}
<a href="/stu/all_stu/?g_id={{ grade.id }}">
班级名称:{{ grade.g_name }}
</a>
<br>
{% endfor %}
3.exclude method - returns all the information that the query result does not meet the conditions
def all_stu(request):
#用GET.get来接收班级表的信息
g_id = request.GET.get('g_id')
students = Student.objects.exclude(g_id=g_id)
return render(request, 'students.html', {'students': students})
Supporting the use of
{% for grade in grades %}
班级ID:{{ grade.id }}
<a href="/stu/all_stu/?g_id={{ grade.id }}">
班级名称:{{ grade.g_name }}
</a>
<br>
{% endfor %}