2012-09-19 35 views
1

,我發現了以下錯誤:的jQuery造成TemplateSyntaxError在Django

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> 
    {% } %} 

我使用jQuery的文件上傳(jQuery UI的版本)插件。任何人都可以向我解釋我如何解決這個問題?

回答

3

喲它是pythonic,所以它迭代就像循環python:http://docs.python.org/tutorial/controlflow.html#for-statements 因此,for語句看起來更像{% for file in o.files %},而不是你的C語法for循環。

另見Django官方文檔for循環:https://docs.djangoproject.com/en/dev/ref/templates/builtins/#for

在你的代碼的其餘部分更仔細地觀察,好像你在你的模板代碼有很多奇怪的語法(如{%=file.name%}應該是{% file.name %}{% if (!o.options.autoUpload) { %}應該是{% if not o.options.autoUpload %}等),你可能會檢查djangobook一章模板: http://djangobook.com/en/2.0/chapter04/

編輯:源使用模板代碼的語法從JavaScript Templates,沒有Django的,這似乎是問題。

+0

當從https://github.com/blueimp/jQuery-File-Upload/tree/jquery-ui下載時,奇怪的語法已經存在了 - 我不知道它是什麼或爲什麼它在那裏。 – user1086337

+0

嗯。我必須更多地查看插件的源代碼,以確切瞭解到底發生了什麼以及哪些內容可以更改,但django的模板語法相當簡單,所以我建議更改模板中的語法標籤('{%these%}')與上述鏈接中指出的內容相關聯,然後繼續解決兼容性問題。 – Kapura

+1

看起來模板代碼不是django,而是[JavaScript模板](http://blueimp.github.com/JavaScript-Templates/)。以下是關於相同代碼的較舊問題:http://stackoverflow.com/questions/10025856/what-is-x-tmpl – Kapura

相關問題