注册信息
1、编辑urls.py路径
path('register/',views.register ),
2、编写views.py视图页面操作
def register(request): if request.method=='GET':form= RegisterModeForm()return render(request,'register.html',{'form':form})form=RegisterModeForm(data=request.POST)if form.is_valid():print(form.cleaned_data)form.save()else:# print(form.errors)return render(request,'register.html',{'form':form})return redirect('/userlist/')
3、编辑useredit.html
{% extends "layout.html" %}
{% block title %} 我的第一个继承自layout.html的文件{% endblock title %}
{% block mybody %}
<div class="container tb-5"><form action="" method="post" novalidate>{% csrf_token %}<h2>用户注册</h2>{% for item in form %}<div class="mb-3"><label for="" class="form-label">{{item.label}}</label>{{item}}<span style="color: red">{{item.errors}}</span></div>{% endfor%}<button type="submit" class="btn btn-primary">提交</button></form>
</div>
{% endblock mybody %}
修改信息
1、跳转编辑页面
<a class='btn btn-primary btn-xs' href='user/{{obj.id}}/edit' >编辑</a>
2、编辑urls.py路径
path('user/<int:nid>/edit/',views.useredit ),
3、编写views.py视图页面操作
def useredit(request,nid):userinfo=models.UserInfo.objects.filter(id= nid).first()if request.method=='GET':form1 = UserModeForm(instance=userinfo)return render(request,'useredit.html',{'userinfo': form1})form1 = UserModeForm(data=request.POST, instance=userinfo)if form1.is_valid():#html 文件外的数据可以通过下面的方法添加# form1.instance.name='东方今典'form1.save()return redirect('/userlist/')else:return render(request,'useredit.html',{'userinfo': form1})
4、编辑useredit.html
{% extends "layout.html" %} {% block mybody %}
<div class="container tb-5"><form action="" method="post" novalidate>{% csrf_token %}<h2>编辑用户</h2>{% for item in userinfo %}<div class="mb-3"><label for="" class="form-label">{{item.label}}</label>{{item}}<span style="color: red">{{item.errors}}</span></div>{% endfor%}<button type="submit" class="btn btn-primary">更新</button></form>
</div>
{% endblock mybody %}
删除信息
1、修改超链接信息
<a class='btn btn-danger btn-xs' href='/user/{{obj.id}}/delete'>删除</a>
2、编辑urls.py路径
path('user/<int:nid>/delete/',views.userdel ),
3、编写views.py视图页面操作
def userdel(request,nid):models.UserInfo.objects.filter(id=nid).delete()return redirect('/userlist/')