渲染不会渲染到模板(The render do not render to the template)
在管理索引页面中,我将id绑定到一个按钮,并使用jquery ajax来请求注销事件:
$("#logout").click(function(){ $.ajax({ url:'/logout/', type:'POST' }) })在frontend / views.py中:
def logout(request): if request.method == 'POST': request.session['username'] = None request.session['is_login'] = False import app_admin.views as app_admin_views app_admin_views.conn = None # clean the connection print ('before logout') return render(request,'frontend/login.html')在终端已打印'注销前',但页面没有呈现到frontend/login.html ,我也尝试使用重定向,全部失败。
In the admin index page I bind a id to a button, and use jquery ajax to request a logout event:
$("#logout").click(function(){ $.ajax({ url:'/logout/', type:'POST' }) })And in the frontend/views.py:
def logout(request): if request.method == 'POST': request.session['username'] = None request.session['is_login'] = False import app_admin.views as app_admin_views app_admin_views.conn = None # clean the connection print ('before logout') return render(request,'frontend/login.html')In the Terminal have printed the 'before logout', but the page do not render to the frontend/login.html, and I also tried use redirect, all failure.
最满意答案
在注销视图功能中,返回重定向
return redirect('login-or-something')在javascript中,AJAX请求处理重定向响应,
function handleSuccess(data, textStatus, jqXHR) { location.href = jqXHR.getResponseHeader('Location'); } function handleError(jqXHR, textStatus, errorThrown) { console.log(errorThrown); // send to some error log collectors } $.ajax({ url:'/logout/', type:'POST' success: handleSuccess, error: handleErr });In logout view function, return a redirect
return redirect('login-or-something')In javascript AJAX request handle the redirect response,
function handleSuccess(data, textStatus, jqXHR) { location.href = jqXHR.getResponseHeader('Location'); } function handleError(jqXHR, textStatus, errorThrown) { console.log(errorThrown); // send to some error log collectors } $.ajax({ url:'/logout/', type:'POST' success: handleSuccess, error: handleErr });更多推荐
发布评论