当前位置:网站首页>Add, delete, modify and check the connection between the two tables
Add, delete, modify and check the connection between the two tables
2022-07-19 01:23:00 【Wbig】
Add model class
stay model.py Add model classes in , Add foreign key , And generate migration , Run migration .
from django.db import models
# Create your models here.
class Major(models.Model):
major_name = models.CharField(max_length=20,verbose_name=' Professional name ')
class Meta:
verbose_name = ' Professional watch '
verbose_name_plural = verbose_name
db_table = 'major'
def __str__(self):
return self.major_name
class Student(models.Model):
stu_name = models.CharField(max_length=20,verbose_name=' full name ')
score = models.IntegerField(verbose_name=' achievement ')
sex = models.CharField(max_length=3,verbose_name=' Gender ')
# Add foreign key
major = models.ForeignKey(Major,on_delete=models.CASCADE,verbose_name=' Major number ')
class Meta:
verbose_name = ' Student list '
verbose_name_plural = verbose_name
db_table = 'student'
def __str__(self):
return self.stu_name
stay admin.py Register model classes in . Sign up for super users , And add data
from django.contrib import admin
from student.models import Major,Student
# Register your models here.
admin.site.register(Major)
admin.site.register(Student)
# Register the super user in the terminal
python manage.py createsuperuser
Display page
Show professional pages
Views.py
class MajorView(View):
def get(self,request):
major_data = Major.objects.all()
content = {
'majors' : major_data
}
return render(request,'major.html',content)
Register routes on urls.py
from django.urls import path
from student import views
urlpatterns = [
path('major/',views.MajorView.as_view()),
]
Display page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3> Professional watch </h3>
<table border="1px" width="200px">
<tr>
<th> Number </th>
<th> Major name </th>
</tr>
{% for major in majors %}
<tr>
<th>{
{ major.id }}</th>
<th><a href="/student/{
{ major.id }}/">{
{ major.major_name }}</a></th>
</tr>
{% endfor %}
</table>
</body>
</html>
Show the student page
class StudentView(View):
def get(self,request,id):
student_data = Student.objects.filter(major_id = id)
content = {
'students' : student_data
}
return render(request,'student.html',content)
Register routes on urls.py
from django.urls import path
from student import views
urlpatterns = [
path('student/<int:id>/',views.StudentView.as_view()),
]
Display page
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h3> Student list </h3>
<table border="1px" width="500px">
<tr>
<th> Number </th>
<th> full name </th>
<th> achievement </th>
<th> Gender </th>
<th> Major number </th>
<th> operation </th>
</tr>
{% for student in students %}
<tr>
<th>{
{ student.id }}</th>
<th>{
{ student.stu_name }}</th>
<th>{
{ student.score }}</th>
<th>{
{ student.sex }}</th>
<th>{
{ student.major }}</th>
<th>
<a href="/delete/{
{ student.id }}/"> Delete </a>
<a href="/update/{
{ student.id }}/"> modify </a>
</th>
</tr>
{% endfor %}
</table>
</body>
</html>
Add, delete, modify and check
increase
You can write at the bottom of the professional page post Method
def post(self,request):
name = request.POST.get('name')
score = request.POST.get('score')
sex = request.POST.get('sex')
major = request.POST.get('major')
try:
Student.objects.create(
stu_name = name,
score = score,
sex = sex,
major_id = major
)
except Exception as e:
print(e)
return HttpResponse(' Add failure ')
return redirect('/major/')
Write the add form at the bottom of the professional page
<form method="post">
{% csrf_token %}
full name :<input type="text" name="name"><br>
achievement :<input type="text" name="score"><br>
Gender :<input type="text" name="sex"><br>
Major number :<input type="text" name="major"><br>
<input type="submit" value=" add to ">
</form>
Delete
class DeleteView(View):
def get(self,request,id):
Student.objects.filter(id=id).delete()
return redirect('/major/')
Register dynamic routing
from django.urls import path
from student import views
urlpatterns = [
path('delete/<int:id>/',views.DeleteView.as_view()),
]
Add delete operation at the back of the student page
<a href="/delete/{
{ student.id }}/"> Delete </a>
modify
class UpdateView(View):
def get(self,request,id):
try:
student_data = Student.objects.get(id = id)
content = {
'students' : student_data
}
except Exception as e:
print(e)
return HttpResponse(' Failed to get data ')
return render(request,'update.html',content)
def post(self,request,id):
name = request.POST.get('name')
score = request.POST.get('score')
sex = request.POST.get('sex')
major = request.POST.get('major')
try:
Student.objects.filter(id=id).update(
stu_name = name,
score = score,
sex = sex,
major = major
)
except Exception as e:
print(e)
return HttpResponse(' Modification failed ')
return redirect('/major/')
Register dynamic routing
from django.urls import path
from student import views
urlpatterns = [
path('update/<int:id>/',views.UpdateView.as_view())
]
Add delete operation at the back of the student page
<a href="/update/{
{ student.id }}/"> modify </a>
边栏推荐
猜你喜欢
随机推荐
Day05-Cookie,Session,Csrf
Oracle 数据库启用归档日志模式和归档日志删除和生成频率
Hutool official graphics
Oracle 数据库架构
@ConfigurationProperties注解使用
RedisTemplate无法根据key 获取值的问题
Calculation method of vector in torch
深度之眼三——(6)】数学:矩阵对角化及二次型1
20210419-组合总和
Record a bug that failed to pass the authentication of calling feign interface in the scheduled task
Watermelon book chapter 4
记录一次多个环境导致代码bug问题
Swagger
Introduction to MySQL DLJD Lao Du
JVM内存模型
kq-权限控制
JSP basic grammar experiment
注册表劫持触发恶意程序
2021-3-23-美团-正则序列
集技术、艺术、运动于一身的个人博客。
![记录BUUCTF [网鼎杯2018]Unfinish1解题思路](/img/29/6cf1eb89f1cbe087438645668ce103.png)





![BUUCTF [BJDCTF2020]EzPHP1详解](/img/10/d2593dff2f4a5f0ca41a9f651128df.png)


