简体   繁体   English

函数无法识别页面变量

[英]Function does not recognize page variables

I have 2 pages that has the logout functionality within it...Currently the html page looks as below...The test1.js has the Logout function and whenever the user clicks the logout button the Logout function is called.. The logout function calls a post request in the server passing the session id and csrf token... 我有2个页面,其中具有注销功能...当前html页面如下所示:test1.js具有注销功能,每当用户单击注销按钮时,就会调用注销功能。.注销功能通过会话ID和csrf令牌调用服务器中的发布请求...

Problem: 问题:

In this current setup, I'm unable to get the csrf and session id from the pages but when I move it inside the document.ready function of individual page it works fine.. 在此当前设置中,我无法从页面获取csrf和会话ID,但是当我将其移动到单个页面的document.ready函数中时,它可以正常工作。

Why the session id and csrf function is not visible if its separate.. 如果会话ID和csrf函数分开,为什么不可见。

Page1.html Page1.html

    <script src="test1.js"></script>
    <form id="searchform" method="post" action="Search">
        <input type="hidden" name="CSRF" value="{{acsrf}}" />
        <input type="hidden" name="ses_id" value="{{session_id}}" />
        <div>
            <input name="search_string" id="search_string" type="text">
            <input id="searchbutton" name="submit" value="Search" class="input_submit" type="submit">
        </div>
    </form>


    <ul>
      <li id="logoutLink"><a href="#">Logout</a></li>
    </ul>

    $(document).ready(function(){
      $('#logoutbutton').click(Logout)     
    })

Page2.html Page2.html

<script src="test1.js"></script>

<ul>
  <li id="logoutLink"><a href="#">Logout</a></li>
</ul>

$(document).ready(function(){
  $(#logoutbutton).click(Logout)     
})

test1.js test1.js

function Logout() {
      $.post("Logout",
       { antiCSRF: '{{acsrf}}',
         session_id: '{{session_id}}'
    });
      alert("You are now logged out.");
      window.location = './';

}

The problem is that your test1.js file cannot access the values from the django template. 问题是您的test1.js文件无法访问django模板中的值。 If you put your javascript inline it should work but adding an external script django treat it as an static file. 如果将javascript内联,它应该可以工作,但是添加外部脚本django会将其视为静态文件。

This post could be helpful 这篇文章可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM