简体   繁体   English

为什么我不能提醒从Django render_to_response返回的字符串

[英]why i can't alert this string which is return from django render_to_response

django code: django代码:

return render_to_response(template_name, {
        "form": form,
    }, context_instance=RequestContext(request))

and html: 和html:

<script type="text/javascript">
        var a='{{form}}'

       alert(a) 
</script>

it's error is 'unterminated string literal', 错误是“未终止的字符串文字”,

and i see this in firebug : 我在萤火虫中看到了这个:

<script type="text/javascript">
        var a='"<tr><th><label for="id_username">Username:</label></th><td><input id="id_username" type="text" class="textinput" name="username" maxlength="30" /></td></tr><tr><th><label for="id_email">Email (optional):</label></th><td><input id="id_email" type="text" class="textinput" name="email" /></td></tr>"';
            alert(a) 
    </script>

how do i alert the 'form' string . 我该如何提醒'form'字符串。

thanks 谢谢

Maybe try putting in semi-colons at the ends of the lines in your Django template file? 也许尝试将分号放在Django模板文件中各行的末尾?

<script type="text/javascript">
    var a='{{form}}';

    alert(a);
</script>

Odd though, I'm pretty sure semi-colons are optional there. 奇怪的是,我很确定分号是可选的。 Could you do a View Source in Firefox (instead of looking via Firebug), and see what HTML is actually being output by Django? 您能否在Firefox中(而不是通过Firebug进行查看)执行查看源代码,并查看Django实际输出的HTML是什么?

Check the HTML source of the page using "View source" rather than Firebug. 使用“查看源代码”而不是Firebug检查页面的HTML源代码。 I predict your {{form}} value has a line break in it, which will cause the error you're seeing. 我预计您的{{form}}值中会有一个换行符,这将导致您看到的错误。

I am unable to reproduce this error, pasting the above into a javascript console works without problems. 我无法重现此错误,将以上内容粘贴到javascript控制台中不会出现问题。 Is it possible that there is something else done with a ? 难道还有别的东西用做a Is above a simplification or exact javascript? 是上面的简化还是精确的javascript? Maybe you can show the entire HTML? 也许您可以显示整个HTML?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM