Reputation: 53
I have a Django model:
class MyModel(models.Model):
charField = models.CharField(maxLength=200)
textField = models.TextField()
and an HTML template with a JS function that displays charField
and textField
like this:
document.getElementById("someElement").innerHTML = "{{ myObject.charField }}";
document.getElementById("someOtherElement").innerHTML = "{{ myObject.textField }}";
The former works fine, but the latter doesn't. I assume the problem is the fact that it is a TextField because there is no other difference between the two. How can I fix this?
Upvotes: 1
Views: 226
Reputation: 477210
For a <textarea>
you should use .value
, so:
document.getElementById("someOtherElement").value = "{{ myObject.textField }}";
for <div>
, you should ineed use .innerHTML
.
Note that both are not a good idea since Django will HTML encode the values, and thus thus can result in "
, &
, etc. in the values. You should use the |escapejs
template filter [Django-doc]:
document.getElementById("someElement").innerHTML = "{{ myObject.charField|escapejs }}";
document.getElementById("someOtherElement").innerHTML = "{{ myObject.textField|escapejs }}";
Upvotes: 2