【问题标题】:how to render html page after ajax success from django backenddjango后端ajax成功后如何渲染html页面
【发布时间】:2018-11-17 13:49:08
【问题描述】:

我想在 ajax 成功后渲染 html 页面。但我没有得到文件的正确路径。我正在使用 django 后端。我没有使用 django 内置函数进行身份验证。 this is my python code. this is the error this is my project structure

//this is my javascript code

function admin_login(){
    username = document.getElementById("username").value;
    password = document.getElementById("password").value;
    console.log(username+password);
    data={
                    "username":username,
                    "password":password
                }
    $.ajax({
                type: 'POST',
                url: "http://127.0.0.1:8000/admin_login/",
                data:JSON.stringify(data),
                success: function(data) {
                  console.log('sucessfully loggedin')
                  window.location.href = "/chat/templates/user.html";
                }
            });

  }

【问题讨论】:

    标签: javascript django python-3.x


    【解决方案1】:

    您正在尝试重定向到模板。 Django 不是那样工作的。

    无论有没有重定向,来自浏览器的请求总是需要转到由视图处理的 URL。您的重定向应该是 urls.py 中列出的实际 URL。

    另外,鉴于这是一个完全标准的表单,不需要 Ajax,您可以完全删除该脚本。只需让表单以正常方式提交并按照 Django 代码确定的方式进行重定向即可。

    【讨论】:

      猜你喜欢
      • 2016-04-24
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 2018-11-30
      • 2016-10-09
      • 2017-12-10
      相关资源
      最近更新 更多