时间:2021-07-01 10:21:17 帮助过:13人阅读
2 student_list.html上编辑标签
<a href="/app01/edit_student/nid={{ item.id }}" class="glyphicon glyphicon-pencil">编辑</a>|
3 编辑学生url
url(r‘^edit_student/nid=(?P<nid>\d+)$‘, views.edit_student),
4 视图
def edit_student(request, nid): if request.method == "GET": student = models.Student.objects.filter(id=nid).values(‘name‘,‘age‘,‘email‘,‘cls_id‘).first() obj = StudentForm(initial=student) return render(request, ‘app01_edit_student.html‘, {‘obj‘:obj, ‘nid‘: nid}) else: obj = StudentForm(request.POST) if obj.is_valid(): # 更新数据库 models.Student.objects.filter(id=nid).update(**obj.cleaned_data) return redirect(‘/app01/students‘) else: return render(request, ‘app01_edit_student.html‘, {‘obj‘: obj})View Code
5 模板
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css"> <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css"> </head> <body> <div style="width: 500px; margin:0 auto"> <h4 style="margin-left:20px;" >编辑学生</h4> <form class="form-horizontal" action="/app01/edit_student/nid={{ nid }}" method="POST"> {% csrf_token %} <div class="form-group"> <label class="col-sm-2 control-label">姓名:</label> <div class="col-sm-10"> {{ obj.name }}{{ obj.errors.name.0 }} </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">年龄:</label> <div class="col-sm-10"> {{ obj.age }} {{ obj.errors.age.0 }} </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">邮箱:</label> <div class="col-sm-10"> {{ obj.email }} {{ obj.errors.email.0 }} </div> </div> <div class="form-group"> <label class="col-sm-2 control-label">班级:</label> <div class="col-sm-10"> {{ obj.cls_id }} {{ obj.errors.cls_id.0 }} </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" class="btn btn-default" value="提交"/> </div> </div> </form> </div> </body> </html>View Code
[oldboy-django][2深入django]学生管理(Form)-- 编辑(设置input标签属性,设置input标签默认显示值,设置input的类型)
标签:label ane return 更新 dict 输入 charset btn 模板