渲染不会渲染到模板(The render do not render to the template)

编程入门 行业动态 更新时间:2024-10-26 20:28:25
渲染不会渲染到模板(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 });

更多推荐

本文发布于:2023-08-02 02:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1368523.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   render   template

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!