Add a list of roles character (forms.ModelForm):
###############forms.ModelForm##############
Edit the list of roles character:
Edit and add roles can share a template ==== "change.html
role_edit DEF (Request, PK): obj = models.Role.objects.filter (PK = PK) .first () IF not obj: return HttpResponse ( " role does not exist " ) IF request.method == " GET " : Print (PK) form = RoleModelForm (= instance obj) of the object obj ########### incoming input value with default values ############ return the render (Request, ' RBAC / change.html ' , { ' form ' : form, ' title ' : ' edit character '}) form = RoleModelForm(data=request.POST,instance=obj) if form.is_valid(): form.save() return redirect(reverse('rbac:role_list')) return render(request, 'rbac/change.html', {'form':form,'title':'编辑角色'})
{% extends 'layout.html' %} {% block content %} <h3>{{ title }}</h3> <div class="luffy-container"> <form class="form-horizontal" method="post" novalidate> {% csrf_token %} {% for fields in form %} <div class="form-group"> <label class="col-sm-2 control-label"> {{ fields.label }} </label> <div class="col-sm-8"> {{ fields }} <span style="color: red;"> {{ fields.errors.0 }}</span> </div> </div> {% endfor %} <div class="form-group"> <div class="col-sm-offset-2 col-sm-8"> <input type="submit" value="保存" class="btn btn-primary"> </div> </div> </form> </div> {% endblock %}
List of roles delete a role: ###### view all shared a confirmation / cancellation page method #######
Jump page allows users to choose whether to confirm :
role_del DEF (Request, PK): origin_url = Reverse ( ' RBAC: role_list ' ) ############## ############## variables based home page view of a variation IF request.method == ' the GET ' : return the render (Request, ' RBAC / delete.html ' , { ' Cancel ' : origin_url}) ############ models.Role.objects .filter (the above mentioned id = PK) .Delete () return redirect (origin_url) ############# ########## jump back to the main page
{% extends 'layout.html' %} {% block content %} <div class="luffy-container"> <div class="alert alert-danger" role="alert"> <form method="post">##############到 视图的post请求 {% csrf_token %} <p style="font-size: 13px"> <i class= " FA FA-Waring " Aria-hidden = " to true " > delete can not be restored, whether to delete </ I> </ P> <div style = " margin-Top: 20px " > <A the href = " {{ }} the cancel " class = " btn btn btn-SM-default " > cancel </a> #################### jump back to the main page origin_url = reverse ( 'rbac: role_list')
<input type="submit" class="btn btn-danger btn-sm" value="确认">
</div>
</form>
</div>
</div>
{% endblock %}