1

次のエラーが発生します。

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 File Upload(jquery ui version)プラグインを使用しています。誰かが私にこれを修正する方法を説明できますか?

4

1 に答える 1

3

これはPythonであるため、Pythonのforループのように繰り返されます。http://docs.python.org/tutorial/controlflow.html#for-statementsしたがって{% for file in o.files %}、forステートメントはC構文のforループではなく、 より似たものになります。

forループの公式djangoドキュメントも参照してください: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 /

編集:ソースは、問題のように思われるDjangoではなく、JavaScriptテンプレートのテンプレートコード構文を使用しています。

于 2012-09-19T21:51:14.113 に答える