首页 » Python » python3 高级篇 » 正文

Django框架–bootstrap退出登录

一、增加退出按钮:

找到菜单栏位置的代码,增加:

<li><a href="/logout/">
    <button type="button" class="btn btn-danger navbar-btn">注销</button>
</a></li>

后端注销代码:

def logout(request):
    request.session.clear()
    return redirect('/auth/demo')

效果图:

mark

二、当删除页面数据的时候需要进行二次认证:

后端代码:

class Del(View):
	def post(self, request, *args, **kwargs):
		get_id = int(request.POST.get('confirmid'))
		if request.POST.get("confirm_bt"):
			models.Host.objects.filter(id=get_id).delete()
			return redirect('/host/list')
		else:
			return redirect('/host/list')

	def get(self, request, *args, **kwargs):
		Gget_id = int(request.GET.get('id'))
		# models.Host.objects.filter(id=get_id).delete()
		# return redirect('/host/list')
		return render(request, 'confirmdelete.html', locals())

前端页面代码:

{% extends "main.html" %}
{% csrf_token %}
{% block content %}
    <!DOCTYPE html>
    <html>
    <head>
        <h4>是否删除id为:{{ Gget_id }} 的数据:</h4>
    </head>
    <body>
    <form name="search_form" action="" method="POST">
        <input type="hidden" name="confirmid" value="{{ Gget_id }}">
        <input type="submit" name="confirm_bt" value="确认">
        <input type="submit" name="cancel_bt" value="取消">
    </form>
    </body>
    </html>

{% endblock %}

效果展示:

mark

确认就会删除数据,否则就会返回数据页面!

赞 (3)

发表评论