一. ursl.py
from django.conf.urls import url
from app01 import views
urlpatterns = [
# 利用无名有名 反向解析 完成数据的增删改查
url(r'^$', views.user_table, name='home'),
url(r'^edit_table/(\d+)', views.edit_table, name='edit_table'),
url(r'^del_table/(\d+)', views.del_table, name='del_table'),
]
二. views.py
from django.shortcuts import render, reverse, HttpResponse, redirect
from app01 import models
# Create your views here.
def user_table(request):
user_queryset = models.User.objects.all()
return render(request, 'user_table.html', {'user_queryset': user_queryset})
def edit_table(request, user_id):
user_obj = models.User.objects.filter(id=user_id).first()
if request.method == 'POST':
user_dict = {
'username': request.POST.get("username"),
'password': request.POST.get("password"),
'age': request.POST.get("age"),
'salary': request.POST.get("salary"),
'hobbies': request.POST.get("hobbies"),
}
# 方式一:
'''
user_obj.username = user_dict.get('username')
user_obj.password = user_dict.get('password')
user_obj.age = user_dict.get('age')
user_obj.salary = user_dict.get('salary')
user_obj.hobbies = user_dict.get('hobbies')
user_obj.save()
'''
# 方式二:
models.User.objects.filter(id=user_id).update(**user_dict)
return redirect(reverse('home'))
return render(request, 'edit_table.html', locals())
def del_table(request, user_id):
models.User.objects.filter(id=user_id).delete()
return redirect(reverse('home'))
三. templates
1. edit_table.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script><link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"><link href="//netdna.bootstrapcdn.com/font-awscome/4.7.0/css/font-awesome.min.css" rel="stylesheet"><script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script></head><body><div class="container"><div class="row"><div class="col-md-8 col-md-offset-2"><h3 class="text-center">编辑页面<small>哦吼~</small></h3><form action="" method="post"><p> username:<input type="text" class="form-control" name="username" value="{{ user_obj.username }}"></p><p> password:<input type="text" class="form-control" name="password" value="{{ user_obj.password }}"></p><p> age:<input type="text" class="form-control" name="age" value="{{ user_obj.age }}"></p><p> salary:<input type="text" class="form-control" name="salary" value="{{ user_obj.salary }}"></p><p> hobbies:<input type="text" class="form-control" name="hobbies" value="{{ user_obj.hobbies }}"></p><p><input type="submit" class="btn btn-warning btn-block" value="提 交"></p></form></div></div></div><script> $("p,h3").addClass('lead')</script></body></html>
2. user_table.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script><link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet"><link href="//netdna.bootstrapcdn.com/font-awscome/4.7.0/css/font-awesome.min.css" rel="stylesheet"><script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script></head><body><div class="container"><div class="row"><div class="col-md-8 col-md-offset-2"><h3 class="text-center">用户信息表格<small>哦吼~</small></h3><table class="table table-hover table-striped table-bordered"><thead><tr><th>ID</th><th>username</th><th>password</th><th>age</th><th>salary</th><th>hobbies</th><th>action</th></tr></thead><tbody> {% for user in user_queryset %}<tr><td>{{ user.id }}</td><td>{{ user.username }}</td><td>{{ user.password }}</td><td>{{ user.age }}</td><td>{{ user.salary }}</td><td>{{ user.hobbies }}</td><td><a href="{% url 'edit_table' user.id %}" class="btn btn-success btn-xs">编辑</a><a href="{% url 'del_table' user.id %}" class="btn btn-danger btn-xs">删除</a></td></tr> {% endfor %}</tbody></table></div></div></div><script> $("th,td,h3").addClass('lead');</script></body></html>
四. models.py
from django.db import models
# Create your models here.
class User(models.Model):
id = models.AutoField(primary_key=True, verbose_name='主键')
username = models.CharField(max_length=255, verbose_name='用户名')
password = models.CharField(max_length=255, verbose_name='密码')
age = models.IntegerField(verbose_name='年龄')
salary = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='薪资')
hobbies = models.CharField(max_length=255, verbose_name='爱好')