Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/409.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django{{message}}不在Javascript中工作_Javascript_Django_Django Templates - Fatal编程技术网

Django{{message}}不在Javascript中工作

Django{{message}}不在Javascript中工作,javascript,django,django-templates,Javascript,Django,Django Templates,我在Django中输出消息时遇到问题。我有Python,它允许用户输入消息并将其发布到googleappengine。我的目标是在Javascript的比较中使用字符串来输出正确的图像 我有以下Javascript代码 var img = document.createElement("img"); img.src = "images/150.png"; if ({{messages.get().message}} == "hello"){ var src = document.getEl

我在Django中输出消息时遇到问题。我有Python,它允许用户输入消息并将其发布到googleappengine。我的目标是在Javascript的比较中使用字符串来输出正确的图像

我有以下Javascript代码

var img = document.createElement("img"); 
img.src = "images/150.png";
if ({{messages.get().message}} == "hello"){
  var src = document.getElementById("image1");
  src.appendChild(img);
}
我不明白为什么
messages.get().message
不起作用。由于某种原因,它给了我一个解析错误。用于在JSON中发布消息的Python代码如下所示:

endef getJSONMessages(callback):
messages = db.GqlQuery("SELECT * FROM Message ORDER BY timestamp DESC LIMIT 1")
strlist = ""
for message in messages:
    if len(strlist)>0:
        strlist += ',' + message.asJSONString()
    else:
        strlist = message.asJSONString()                  
if callback=='':
    return '[' + strlist + ']'
else:    
    return callback+'([' + strlist + ']);'
如果您能在这个问题上提供帮助,我将不胜感激。

试试看

var img = document.createElement("img"); 
img.src = "images/150.png";
if ("{{messages.get.message|escapejs}}" == "hello"){
  var src = document.getElementById("image1");
  src.appendChild(img);
}
模板中不需要“messages.get()”。“信息,获取”wii do

如果你使用

if(messages.get.message == "hello") { ... }

消息周围将没有引号,这将导致语法错误。

是否
{{{messages.get.message}}
有效?Django模板变量查找中不能有括号,但Django。应用程序现在运行。。但是图像没有被输出,谢谢!!我不敢相信问题出在哪里。。然而,它确实不起作用。我用一个输出测试了它,它打印了消息,但是图像仍然没有输出$(document.ready(function(){var img=document.createElement(“img”);img.src=“images/hello.png”;if({messages.get.message}=“hello”){var src=document.getElementById(“image1”);src.appendChild(img);)这不起作用