Django模板中的JavaScript语法错误,当变量值丢失时 [英] Javascript syntax error in Django Template when variable value is missing

查看:99
本文介绍了Django模板中的JavaScript语法错误,当变量值丢失时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有AJAX菜单的Django模板(点击不同的菜单项重新加载页面的一部分)。每个菜单单击通过AJAX调用Django视图功能,并返回一些要显示在页面上的数据。



我正在加载所有菜单项所需的所有JS主页面(据了解,AJAX在< div> 中返回JS并不是一个好主意,然后使用eval()来执行它)。因为我正在菜单的主页上加载所有的JS,所以数据显然不是开始时的其他菜单选项,因为它将在稍后调用相应的视图(当点击菜单选项时)来到



因为下面的代码给出了控制台上的语法错误,因为JS只是第一次被解析。

 <脚本> var data = {{se_data | safe}}; < /脚本> 

现在我无法使用从视图返回的数据,即使我点击菜单选项,因为JS解析失败了。如果只有在选择了相应的菜单选项时才执行代码,那么我尝试使用,但是由于解析器只是解析所有内容,所以也不行。



我一直在尝试不同的方法来解决问题,但似乎找不到有效的方法来解决这个问题。另一种可能是使视图功能仅返回所有数据,但我担心菜单选项和返回的数据可能随时间而增长,从而延迟主菜单页面的初始加载。



这是ajax菜单的点击功能:

  $(。ent-menu li a ).click(function(e){
e.preventDefault(); //阻止进入href中提到的页面
//获取href
var href = $(this ).attr(href);

$(#data_container)load(href,function(){
load_data(); //此函数使用定义的数据变量早期
});
});

加载菜单主页时,我在控制台看到错误:

 未捕获语法错误:意外的令牌;在第1110行
data =;

由于数据不可用。

解决方案

如上所述,您应该使用JSON将数据从Python发送到JS;使用 JSON.parse()反序列化用于您的JS的数据。


I have a Django template with an AJAX menu (where clicking on different menu items reloads a part of the page). Each menu click invokes a Django view function through AJAX and returns some data to be shown on the page.

I am loading all the JS required for all the menu items in the main page only (as I learnt that it is not a good idea for AJAX to return JS in a <div> and then use eval() to execute it). Since I am loading all the JS on the menu's main page only, the data obviously isn't there for other menu options at start since it will come later when the corresponding view is invoked (when the menu option is clicked)

Because of that, code like the one below gives a syntax error on the console since JS is parsed the very first time only.

<script> var data = {{se_data|safe}}; </script>

Now I am not able to use the data returned from the view, even after I click the menu options, since the JS parsing failed in the first place. I tried using if to execute the code only when the corresponding menu option is selected but that too does not work since the parser just parses everything.

I have been trying different methods to get around the issue but cant seem to find an efficient way to solve this. An alternative could be to have the view function return all the data at first only, but I fear that the menu options and the data returned may grow over time, hence delaying the initial load of the main menu page.

This is the ajax menu's click function:

$(".ent-menu li a").click(function(e) {
    e.preventDefault(); // prevent from going to the page mentioned in href
    // get the href
    var href = $(this).attr("href");

    $("#data_container").load(href, function() {
        load_data(); //this function uses the data variable defined earlier
    });
});

Error I see in the console when loading the menu's main page:

Uncaught SyntaxError: Unexpected token ;     at line 1110
data = ;

since the data is not available yet.

解决方案

As discussed, you should use JSON to send the data from Python to JS; use JSON.parse() to deserialize the data for use in your JS.

这篇关于Django模板中的JavaScript语法错误,当变量值丢失时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆