jQuery在Django中导致TemplateSyntaxError
我得到以下错误:jQuery在Django中导致TemplateSyntaxError,django,jquery-ui,jquery-plugins,django-templates,Django,Jquery Ui,Jquery Plugins,Django Templates,我得到以下错误: Exception Type: TemplateSyntaxError Exception Value: 'for' statements should use the format 'for x in y': for (var i=0, file; file=o.files[i]; i++) { 但我不知道如何在Django中转义,我添加了{%autoescape off%}标记,但仍然没有运气 以下是导致错误的代码: {% for (var i=0, file; file
Exception Type: TemplateSyntaxError
Exception Value: 'for' statements should use the format 'for x in y': for (var i=0, file; file=o.files[i]; i++) {
但我不知道如何在Django中转义,我添加了{%autoescape off%}
标记,但仍然没有运气
以下是导致错误的代码:
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade">
<td class="preview"><span class="fade"></span></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
{% if (file.error) { %}
<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
{% } else if (o.files.valid && !i) { %}
<td>
<div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="bar" style="width:0%;"></div></div>
</td>
<td class="start">{% if (!o.options.autoUpload) { %}
<button class="btn btn-primary">
<i class="icon-upload icon-white"></i>
<span>{%=locale.fileupload.start%}</span>
</button>
{% } %}</td>
{% } else { %}
<td colspan="2"></td>
{% } %}
<td class="cancel">{% if (!i) { %}
<button class="btn btn-warning">
<i class="icon-ban-circle icon-white"></i>
<span>{%=locale.fileupload.cancel%}</span>
</button>
{% } %}</td>
</tr>
{% } %}
</script>
<!-- The template to display files available for download -->
<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-download fade">
{% if (file.error) { %}
<td></td>
<td class="name"><span>{%=file.name%}</span></td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
{% } else { %}
<td class="preview">{% if (file.thumbnail_url) { %}
<a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
{% } %}</td>
<td class="name">
<a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
</td>
<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
<td colspan="2"></td>
{% } %}
<td class="delete">
<button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
<i class="icon-trash icon-white"></i>
<span>{%=locale.fileupload.destroy%}</span>
</button>
<input type="checkbox" name="delete" value="1">
</td>
</tr>
{% } %}
{%for(var i=0,file;file=o.files[i];i++){%}
{%=文件名%}
{%=o.formatFileSize(file.size)%}
{%if(file.error){%}
{%=locale.fileupload.error%}{%=locale.fileupload.errors[file.error]| | file.error%}
{%}如果(o.files.valid&&!i){%}
{%if(!o.options.autoUpload){%}
{%=locale.fileupload.start%}
{% } %}
{%}其他{%}
{% } %}
{%if(!i){%}
{%=locale.fileupload.cancel%}
{% } %}
{% } %}
{%for(var i=0,file;file=o.files[i];i++){%}
{%if(file.error){%}
{%=文件名%}
{%=o.formatFileSize(file.size)%}
{%=locale.fileupload.error%}{%=locale.fileupload.errors[file.error]| | file.error%}
{%}其他{%}
{%if(file.thumbnail_url){%}
{% } %}
{%=o.formatFileSize(file.size)%}
{% } %}
{%=locale.fileupload.destroy%}
{% } %}
我正在使用jQuery文件上传(jQuery ui版本)插件。有人能告诉我如何解决这个问题吗?Yo它是pythonic的,所以它像python中的for循环一样迭代: 因此,for语句看起来更像是o.files%}中的
{%for文件,而不是您的C语法for循环
另请参阅django官方文档了解循环:
更仔细地观察代码的其余部分,您的模板代码中似乎有很多奇怪的语法(例如,{%=file.name%}
应该是{%file.name%}
,{%if(!o.options.autoUpload){%}
应该是{%if not o.options.autoUpload%}
,等等)您可以查看djangobook中有关模板的章节:
编辑:源代码使用的是来自的模板代码语法,而不是Django,这似乎是问题所在。从下载时,奇怪的语法已经存在了——我不知道它是什么,也不知道它为什么存在。嗯。我必须更多地查看插件的源代码,以了解到底发生了什么,什么可以更改,什么不能更改,但是django的模板语法相当简单,因此我建议将模板标记({%this%}
)中的语法更改为上面链接中所示的语法,然后继续解决兼容性问题。看起来模板代码不是针对django的,而是针对同一代码的。下面是一个较老的问题: