Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unterminated string literal error for a django template tag

I have the following snippet of code which gives me unterminated string literal error

$(function() {
             $('#addDropdown').click(function() {
             var $d = $('{{ form |bootstrap }}').fadeIn().delay(1000);   
             $('#dropdownContainer').append($d);
             });
             });

I have looked unterminated string literal question and found that it is because multiline problem.My {{ form |bootstrap }} has multiple lines of code as mentioned in the question.So how do I handle the problem

like image 862
iLoveCamelCase Avatar asked Jun 18 '14 11:06

iLoveCamelCase


People also ask

How do you fix unterminated string constant?

To solve the "Unterminated string constant" error, make sure to enclose your strings in quotes consistently. String literals must be enclosed in single quotes, double quotes or backticks. When writing a multiline string use backticks.

What is an unterminated string literal in Python?

This JavaScript error unterminated string literal occurs if there is string which is not terminated properly. String literals must be enclosed by single (') or double (“) quotes.


1 Answers

In general, to use with JavaScript, you should use |escapejs filter:

var $d = $('{{ value|escapejs }}');

However, as a matter of best practice you'd better put {{ form|bootstrap }} in HTML, and use JavaScript just to fade in:

<div id="djangoForm" style="display:none">{{ form|bootstrap }}</div>
<script>
    $('#addDropdown').click(function() {
         $('#dropdownContainer').append($('#djangoForm'));
    }
</script>
like image 192
David Avsajanishvili Avatar answered Sep 24 '22 11:09

David Avsajanishvili