forms
# 角色的formclass RoleForm(forms.ModelForm): class Meta: model = models.Role fields = '__all__' exclude = ['permissions'] widgets = { 'name':forms.TextInput(attrs={'class':'form-control'}), }
后端
# 角色添加/编辑 def role_change(request, edit_id=None): obj = models.Role.objects.filter(pk=edit_id).first() form_obj = RoleForm(instance=obj) title = '添加角色信息' if not obj else '编辑角色信息' if request.method == 'POST': form_obj = RoleForm(request.POST, instance=obj) if form_obj.is_valid(): form_obj.save() return redirect(reverse('role_list')) return render(request, 'rbac/forms.html', {'form_obj': form_obj, 'title': title})
前端页面
{% extends 'layout.html' %}{% block content %}{% endblock %}{ { title }}