MaSao93
MaSao93

Reputation: 202

Can not get textfield value fetched from MySQL in javascript

I created a model in django like below.

class MyDB(models.Model):
    pub_date = models.DateTimeField(_("Date added"),editable=True, auto_now_add=False)
    title = models.CharField(max_length=256,blank=True,null=True)
    text = models.TextField(_("Text"))

Then I got the model and mapped into Frontend.

in views.py file

context['mydb'] = MyDB.objects.all()

in HTML file

let plots = [];
{% for d in mydb %}
    var id = '{{ d.id }}';
    var title = '{{ d.title }}';
    var text = '{{ d.text }}';        
    console.log(text)
        
{% endfor %}

When the text contains 'enter' in string. it raise error.

enter image description here

Upvotes: 0

Views: 22

Answers (1)

benmotyka
benmotyka

Reputation: 179

Please try to replace ' with ` (backtick), it should fix your issue (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#multi-line_strings)

Upvotes: 1

Related Questions