当前位置:网站首页>Example of single table query in ORM student management system
Example of single table query in ORM student management system
2022-07-24 18:08:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Preparatory work
First, create a project
One : You have to use MySQL Create a library
because ORM Only tables and data can be processed , So the library must be created by itself
create database mysite;Two : Make the relevant configuration
In the project mysite Of settings.py In file One :
Two :
3、 ... and :
Four :
5、 ... and :
3、 ... and Create table
We must pay attention to two points :
stay app/models.py Create a class in the file , Corresponding to the library in the database , The class name is the library name The created class must inherit models.ModelFour : Linked database
In the project __init__.py Import in file pymysql5、 ... and : Execute relevant orders
stay PyCharm Click on the bottom Terminal Execute the following two commands :
1:python manage.py makemigrations Record
2:python manage.py migrate translate Formally write the program
Table structure
class Class(models.Model):
id = models.AutoField(primary_key=True) # Primary key
cname = models.CharField(max_length=32) # Class name
first_day = models.DateField() # Opening time Query class
URL part :
url(r'^class_list/$', views.class_list, name="class_list"),View part :
def class_list(request):
class_list = models.Class.objects.all()
return render(request, "class_list.html", {"class_list": class_list})HTML part :
<table border="1">
{% for class in class_list %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ class.id }}</td>
<td>{{ class.cname }}</td>
<td>{{ class.first_day|date:'Y-m-d' }}</td>
</tr>
{% endfor %}
</table>New classes
URL part :
url(r'^add_class/$', views.add_class, name="add_class"),View part :
def add_class(request):
# front end POST Fill in the new class information
if request.method == "POST":
cname = request.POST.get("cname")
first_day = request.POST.get("first_day")
# You can also get the submitted data in this way , But it is not recommended to write so
# data = request.POST.dict()
# del data["csrfmiddlewaretoken"]
# There are two ways to create new data
# new_class = models.Class(cname=cname, first_day=first_day)
# new_class.save()
models.Class.objects.create(cname=cname, first_day=first_day)
# Jump to class_list
return redirect(reverse('class_list'))
# Return to the page of adding classes
return render(request, "add_class.html")HTML part :
Add a... On the class list page a label :
<a href="{% url 'add_class' %}"> New page add </a>Add new page :
Be careful {% csrf_token %} and date Type of input label .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> Add class </title>
</head>
<body>
<form action="{% url 'add_class' %}" method="post">
{% csrf_token %}
<p> Class name :<input type="text" name="cname"></p>
<p> Start date :<input type="date" name="first_day"></p>
<p> Submit <input type="submit"></p>
</form>
</body>
</html>Delete class
URL part :
url(r'^delete_class/$', views.delete_class, name="delete_class"),View part :
def delete_class(request):
class_id = request.GET.get("class_id")
models.Class.objects.filter(id=class_id).delete()
return redirect(reverse("class_list"))HTML part :
Add or delete in the table on the class list page .
<a href="{% url 'delete_class' %}?class_id={{ class.id }}"> Delete </a>Edit class
URL part :
url(r'^edit_class/$', views.edit_class, name="edit_class"),View part :
def edit_class(request):
if request.method == "POST":
class_id = request.POST.get("id")
cname = request.POST.get("cname")
first_day = request.POST.get("first_day")
models.Class.objects.create(id=class_id, cname=cname, first_day=first_day)
return redirect(reverse("class_list"))
class_id = request.GET.get("class_id")
class_obj = models.Class.objects.filter(id=class_id)
if class_obj:
class_obj = class_obj[0]
return render(request, "edit_class.html", {"class": class_obj})
# The record cannot be found
else:
return redirect(reverse("class_list"))HTML part :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="x-ua-compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> Edit class </title>
</head>
<body>
<form action="{% url 'edit_class' %}" method="post">
{% csrf_token %}
<input type="text" value="{{ class.id }}" style="display: none">
<p> Class name :<input type="text" name="cname" value="{{ class.cname }}"></p>
<p> Start date :<input type="date" name="first_day" value="{{ class.first_day|date:'Y-m-d' }}"></p>
<p> Submit <input type="submit"></p>
</form>
</body>
</html>Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/124474.html Link to the original text :https://javaforall.cn
边栏推荐
- Section 11 cache avalanche, hot data failure follow Daewoo redis ------- directory post
- Inheritance and Derive
- Laravel notes - RSA encryption of user login password (improve system security)
- 获取同程(艺龙)酒店数据
- Three ways of redis cluster
- new也可以创建对象,为什么需要工厂模式?
- [leetcode] 30. Concatenate substrings of all words
- Is header file required? Follow the compilation process~~~
- 0621~ES&Lucene
- Use prometheus+grafana to monitor MySQL performance indicators
猜你喜欢

T245982 "kdoi-01" drunken flower Yin

Bib | mol2context vec: context aware deep network model learning molecular representation for drug discovery

Inheritance and Derive

Opencv picture rotation

Section 7 Data Dictionary: hash followed by Daewoo redis ------- directory post

In depth analysis of the famous Alibaba cloud log4j vulnerability

【obs】依赖库: x264 vs 构建

Inherit, override, overload

0625~<config>-<bus>

Emerging potential of interactive virtual reality technology in drug discovery
随机推荐
com.mysql.cj.jdbc.exceptions. MySQLTransactionRollbackException: Deadlock found when trying to get lo
213. Looting II - Dynamic Planning
How to prepare for hyperinflation
0621~ES&Lucene
Goodbye Navicat! This open source database management tool has a cooler interface!
手写博客平台~第二天
Review and analysis of noodle dishes
New can also create objects. Why do you need factory mode?
C language custom types - Enumeration
Common questions of testers during interview
阿里巴巴/1688按图搜索商品(拍立淘) API使用说明
Introduction and use of Pinia
Inherit, override, overload
new也可以创建对象,为什么需要工厂模式?
Model saving and loading of sklearn
Just one dependency to give swagger a new skin, which is simple and cool!
船新 IDEA 2022.2 正式发布,新特性真香!
0627~ holiday knowledge summary
0611~ self study class
Use prometheus+grafana to monitor MySQL performance indicators