您好,登錄后才能下訂單哦!
怎么在Django中創(chuàng)建和刪除用戶?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
html:
{# 用戶管理 #} <div id="userManageDiv" > <div id="" > {# 創(chuàng)建用戶 #} <h5 >創(chuàng)建用戶</h5> <hr /> <div class="input-group" > <span class="input-group-addon" id="basic-addon1" >用戶名</span> <input type="text" class="form-control" id="username" name="username" placeholder="請(qǐng)輸入用戶名" aria-describedby="basic-addon1" /> </div> <div class="input-group" > <span class="input-group-addon" id="basic-addon2" >密碼</span> <input type="password" class="form-control" id="password" name="password" placeholder="請(qǐng)輸入密碼" aria-describedby="basic-addon2" /> </div> <div class="input-group" > <span class="input-group-addon" id="basic-addon3">確認(rèn)密碼</span> <input type="password" class="form-control" id="passwordAgain" name="passwordAgain" placeholder="請(qǐng)?jiān)俅屋斎朊艽a" aria-describedby="basic-addon3" /> </div> <div class="input-group" > <span class="input-group-addon" >是否激活</span> <select name="userStatus" id="userStatus" class="form-control selectpicker"> <option value="1">激活</option> <option value="0">封停</option> </select> </div> <div id="createUserAlert" class="alert alert-danger" role="alert" ></div> <button type="button" id="createUserBtn" class="btn btn-default" data-toggle="modal" data-target="#alertTip" data-whatever="激活/封停 該賬戶?" >提 交</button> <hr /> </div> <div id="" > {# 刪除用戶 #} <h5 >刪除用戶</h5> <hr /> <div class="input-group" > <span class="input-group-addon" >用戶名</span> <select name="deleteUsername" id="deleteUsername" class="form-control selectpicker" data-live-search="true"> {% for user in users %} <option value="{{ user.username }}">{{ user.username }}</option> {% endfor %} </select> </div> <button type="button" id="deleteUserBtn" class="btn btn-default" data-toggle="modal" data-target="#alertTip" data-whatever="刪除 該賬戶?" >提 交</button> </div> </div>
html–模態(tài)框:
{# 模態(tài)框 #} <div class="modal fade" tabindex="-1" role="dialog" id="alertTip"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> <h5 class="modal-title">確認(rèn)提示框??!</h5> </div> <div class="modal-body"> <p id="modal_content"></p> </div> <div class="modal-footer"> <button type="button" class="btn btn-primary" id="modalConfirmBtn">確認(rèn)</button> <button type="button" class="btn btn-default" data-dismiss="modal">取消</button> </div> </div> </div> </div>
前端js代碼:
// 模態(tài)框 body顯示 $(document).ready(function () { $('#alertTip').on('show.bs.modal', function (e) { var button = $(e.relatedTarget); var content = button.data('whatever'); var modal = $(this); modal.find('#modal_content').text('確認(rèn) ' + content); }) }); // 模態(tài)框 顯示前,記錄 哪個(gè)div 啟動(dòng)了 模態(tài)框 var preAjaxDiv; $(document).ready(function () { $('#createUserBtn').click(function () { preAjaxDiv = $(this).attr('id'); }); $('#deleteUserBtn').click(function () { preAjaxDiv = $(this).attr('id'); }); $('#changePasswordBtn').click(function () { preAjaxDiv = $(this).attr('id'); }); $('#changePermissionBtn').click(function () { preAjaxDiv = $(this).attr('id'); }); }); // 模態(tài)框 點(diǎn)擊確認(rèn)后執(zhí)行 $(document).ready(function () { $('#modalConfirmBtn').click(function () { if (preAjaxDiv == 'createUserBtn') { createUser(); $('#alertTip').modal('hide'); } else if (preAjaxDiv == 'deleteUserBtn') { deleteUser(); $('#alertTip').modal('hide'); } else if (preAjaxDiv == 'changePasswordBtn') { changePassword(); $('#alertTip').modal('hide'); } else if (preAjaxDiv == 'changePermissionBtn') { changePermission(); $('#alertTip').modal('hide'); } }) }); // 創(chuàng)建用戶 激活/封停 function createUser() { var createUserAlert = ''; $('#createUserAlert').hide(); if ( !$('#username').val() ) { createUserAlert += "** 用戶名不能為空!<br />"; } if ( !$('#password').val() ) { createUserAlert += "** 密碼不能為空!<br />"; } if ( !$('#passwordAgain').val() ) { createUserAlert += "** 確認(rèn)密碼不能為空!<br />"; } if ( $('#password').val() != $('#passwordAgain').val() ) { createUserAlert += "** 兩次密碼輸入不一致!<br />"; } if (createUserAlert) { $('#createUserAlert').html(createUserAlert); $('#createUserAlert').show(); } else { $.ajax({ url: '/createUser', type: 'POST', data: { createUsername: $('#username').val(), password: $('#password').val(), is_active: $('#userStatus').val() }, success: function (data, textStatus) { if (data == 1) { alert('創(chuàng)建成功!'); window.location.href = 'slg_manager_tem'; } else if (data == -1) { alert('已經(jīng)創(chuàng)建該賬戶,無法重復(fù)創(chuàng)建!'); } }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); } }) } } // 刪除用戶 function deleteUser() { $.ajax({ url: '/deleteUser', type: 'POST', data: { deleteUsername: $('#deleteUsername').val() }, success: function (data, textStatus) { if (data == 1) { alert('刪除成功!'); window.location.href = 'slg_manager_tem'; } }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(errorThrown); } }) }
后端python–view視圖:
# 創(chuàng)建用戶 激活/封停 @login_required(login_url='slg:login') @require_http_methods(["POST"]) @permission_required('slg.views_slg_manager_tem', login_url='slg:get_permissionDenied') def create_user(request): username = request.POST['createUsername'] password = request.POST['password'] is_active = request.POST['is_active'] createResult = db_create_user(username, password, is_active) if createResult == 1: return HttpResponse('1') elif createResult == -1: return HttpResponse('-1') # 刪除用戶 @login_required(login_url='slg:login') @require_http_methods(["POST"]) @permission_required('slg.views_slg_manager_tem', login_url='slg:get_permissionDenied') def delete_user(request): username = request.POST['deleteUsername'] deleteResult = db_delete_user(username) if deleteResult: return HttpResponse('1')
后端python–models視圖:
from django.contrib.auth.models import User from django.contrib.auth import authenticate from django.db.utils import IntegrityError from . import db_models as db # 創(chuàng)建用戶 激活/封停 def db_create_user(username, password, is_active): if is_active == '0': is_active = False elif is_active == '1': is_active = True try: User.objects.create_user(username=username, password=password, is_active=is_active) except IntegrityError: return -1 # 已經(jīng)創(chuàng)建,無法重復(fù)創(chuàng)建 else: return 1 # 創(chuàng)建成功 # 刪除用戶 def db_delete_user(username): deleteResult = User.objects.filter(username=username).delete() if deleteResult: return 1
關(guān)于怎么在Django中創(chuàng)建和刪除用戶問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。