【问题标题】:Template syntax error - Could not parse the remainder模板语法错误 - 无法解析剩余部分
【发布时间】:2014-07-13 23:55:39
【问题描述】:

我尝试在 Django 中使用 Jquery 创建一个 div,但出现以下错误 -

无法解析剩余部分:来自 '\'product-images/' 的 '\'product-images/'

$("<div id = product" + i + "left  class = product-cards-left> <a href=\'{% static \'product-images/ " + obj.image_caption +  " \' %}\' data-lightbox=\'image-1\' data-title=\'My caption\''>Image #1</a> </div>").appendTo('#product' + i)

基本上我有一个 id,然后是一个“a”标签。

我该如何解决这个问题?

最新代码:--

$("<div id = product" + i + "left  class = product-cards-left>  <a href={\% static \'product-images///" + obj.image_caption + "' \%}\'      >Image #1</a> </div>").appendTo('#product' + i)


<div id="product0left" class="product-cards-left">  <a href="{%" static="" 'product-images="" 10000_books'="" %}'="">Image #1</a> </div>

“product-images”和“1000_books”之间的“/”没有被添加

【问题讨论】:

  • 试试这个(你不需要转义',除非它是嵌套的):$("&lt;div id = product" + i + "left class = 'product-cards-left'&gt; &lt;a href='{{STATIC_URL}}/product-images/{{obj.image_caption}}' data-lightbox='image-1' data-title='My caption'&gt;Image #1&lt;/a&gt; &lt;/div&gt;").appendTo("#product" + i)
  • 该页面是使用 Jquery 生成的。您的语法看起来非常适合 HTML...
  • 那么 django 标签在 jquery 页面中不起作用。您正在尝试的方法行不通。
  • 我有多个产品需要在页面上显示。产品是从 ajax 调用返回的,我使用 Jquery 生成页面...静态关键字用于从静态网络服务器...
  • 我检查了这个文档 - nomadjourney.com/2009/01/… - 看起来我们可以在 Jquery 中加载模板标签..有什么想法吗?

标签: jquery django


【解决方案1】:

我不建议使用 jQuery 来构建您的页面。 Dom 操作非常繁重,而 Django 在这方面更好/更快。

另外我建议首先拆分你的 div 和链接(特别是在调试期间):

var div = $('<div>', {
  id: 'product' + i + 'left',
  class: 'product-cards-left'
});

$('<a>', {
  href: '{{STATIC_URL}}/product-images/{{obj.image_caption}}',
  'data-lightbox': 'image-1',
  'data-title': 'My caption',
  text: 'Image #1'
}).appendTo(div);

div.appendTo('#product' + i);

【讨论】:

    猜你喜欢
    • 2016-04-03
    • 2019-09-04
    • 2011-04-03
    • 1970-01-01
    • 2011-11-06
    • 2013-10-26
    • 2018-03-31
    • 2020-05-10
    • 2017-11-18
    相关资源
    最近更新 更多