一、orm的增删查改
orm创建表如下:
class User(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
password = models.CharField(max_length=32)
adder=models.CharField(max_length=64,default='上海')
查:可以使用(render(request,'user_list.html','lian_list':set))获得
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主表</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
<th>adder</th>
</tr>
</thead>
<tbody>
{% for lian in lian_list %}
<tr>
<td>{{ lian.id }}</td>
<td>{{ lian.name }}</td>
<td>{{ lian.password }}</td>
<td>{{ lian.adder }}</td>
<td> <a href="/del_list/?id={{ lian.id }}">删除</a></td>
<td> <a href="/update_list/?id={{ lian.id }}">改变</a></td>
</tr>
{% endfor %}
</tbody>
<a href="/add_list/">添加</a>
</table>
</body>
</html>
user_list主页代码
def user_list(request):
if request.method == 'GET':
ret = models.User.objects.all()
return render(request, 'user_list.html', {'lian_list': ret})
views中user_list代码
增:可以直接在models中创建的User表中添加一个字段adder后执行Python3 manag.py makemigrations 与python3 manage.py migrate
有两种方法分别为
1.models.User.objects.create(name=name,password=password,adder=adder)
2.res=models.User(name=name,password=password,adder=adder)
res.argv()
def update_list(request):
if request.method=='GET':
return render(request,'update_list.html')
elif request.method=='POST':
id = request.GET.get('id')
name=request.POST.get('name')
password=request.POST.get('password')
adder=request.POST.get('adder')
models.User.objects.filter(id=id).update(name=name,password=password,adder=adder)
return redirect('/user_list/')
views中update_list代码
删:可以分别执行以下代码
在user_list.html中<tobody>下<tr>下<td> <a href="/del_list/?id={{ lian.id }}">删除</a></td>
View Code
def del_list(request):
if request.method == 'GET':
id = request.GET.get('id')
models.User.objects.filter(id=id).delete()
return redirect('/user_list/')
views中del_list代码
改:方法为(models.User.objects.filter(id=id).update(name=name,password=password,adder=adder))
def add_list(request):
if request.method == 'GET':
return render(request, 'add_list.html')
elif request.method == 'POST':
name = request.POST.get('name')
password = request.POST.get('password')
adder = request.POST.get('adder')
models.User.objects.create(name=name, password=password, adder=adder)
return redirect('/user_list/')
views中add_list代码
二、user的增删查改
***重点****:
1 单表查询所有用户:models.User.objects.all()
得到的是 queryset对象(当成列表),列表里面,一个一个的对象[user1,user2]
2 render(request, 'userlist.html', {'user_list': ret})
3 模板里: {% for user in user_list %}
#要循环的内容
{{user.name}}
{% endfor%}
4 get请求携带参数:
http://127.0.0.1:8000/deleteuser/?id=1
后台取值:request.GET.get('id')
request.GET['id']
5 orm删除记录 models.User.objects.filter(id=id).delete()
返回值:影响的行数
6 前台post提交的数据取值:name=request.POST.get('name')
7 orm保存:
两种方式:
1 user=models.User.objects.create(name=name,password=pwd,address=addr)
2 user=models.User(name=name,password=pwd,address=addr)
user.save()
8 orm查询单条数据:user=models.User.objects.filter(id=id).first()
9 orm的修改 models.User.objects.filter(id=id).update(name=name,password=pwd,address=addr)
演示如下:
"""lianxi URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^user_list/', views.user_list),
url(r'^del_list/',views.del_list),
url(r'^add_list/',views.add_list),
url(r'^update_list/', views.update_list),
]
urls路由
from django.shortcuts import render, redirect
from app01 import models
# Create your views here.
def user_list(request):
if request.method == 'GET':
ret = models.User.objects.all()
return render(request, 'user_list.html', {'lian_list': ret})
def del_list(request):
if request.method == 'GET':
id = request.GET.get('id')
models.User.objects.filter(id=id).delete()
return redirect('/user_list/')
def add_list(request):
if request.method == 'GET':
return render(request, 'add_list.html')
elif request.method == 'POST':
name = request.POST.get('name')
password = request.POST.get('password')
adder = request.POST.get('adder')
models.User.objects.create(name=name, password=password, adder=adder)
return redirect('/user_list/')
def update_list(request):
if request.method=='GET':
return render(request,'update_list.html')
elif request.method=='POST':
id = request.GET.get('id')
name=request.POST.get('name')
password=request.POST.get('password')
adder=request.POST.get('adder')
models.User.objects.filter(id=id).update(name=name,password=password,adder=adder)
return redirect('/user_list/')
views视图函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>主表</title>
</head>
<body>
<table border="1">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
<th>adder</th>
</tr>
</thead>
<tbody>
{% for lian in lian_list %}
<tr>
<td>{{ lian.id }}</td>
<td>{{ lian.name }}</td>
<td>{{ lian.password }}</td>
<td>{{ lian.adder }}</td>
<td> <a href="/del_list/?id={{ lian.id }}">删除</a></td>
<td> <a href="/update_list/?id={{ lian.id }}">改变</a></td>
</tr>
{% endfor %}
</tbody>
<a href="/add_list/">添加</a>
</table>
</body>
</html>
templates下user_list
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>改变</title>
</head>
<body>
<form action="" method="post">
<p>用户名: <input type="text" name="name"></p>
<p>密码: <input type="password" name="password"></p>
<p>地址: <input type="text" name="adder"></p>
<input type="submit" value="改变">
</form>
</body>
</html>
templates下update_list
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加</title>
</head>
<body>
<form action="" method="post">
<p>用户名: <input type="text" name="name"></p>
<p>密码: <input type="password" name="password"></p>
<p>地址: <input type="text" name="adder"></p>
<input type="submit" value="提交">
</form>
</body>
</html>
templates下add_list