开发者

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

开发者 https://www.devze.com 2022-12-20 08:43 出处:网络
django code: return render_to_response(template_name, { \"form\": form, }, context_instance=RequestContext(request))

django code:

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

and html:

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

       alert(a) 
</script>

it's err开发者_StackOverflow中文版or 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 .

thanks


Maybe try putting in semi-colons at the ends of the lines in your Django template file?

<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?


Check the HTML source of the page using "View source" rather than Firebug. I predict your {{form}} value has a line break in it, which will cause the error you're seeing.


I am unable to reproduce this error, pasting the above into a javascript console works without problems. Is it possible that there is something else done with a? Is above a simplification or exact javascript? Maybe you can show the entire HTML?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号