Hey,
I have a model form textfield, if I submit the text and display it in the template every space symbol gets removed like theres just one left then. How can I change that every symbol from the form gets displayed in the template?
Template:
<section>
<div class="codeshare">
<span><h1>{{ title }}</h1> <h5>{{ created_at }} | {{ views }} Views</h5></span>
<div class="cs_code">
<p>{{ code|linebreaks }}</p>
</div>
</div>
</section>
Form:
class CodeShareForm(ModelForm):
def __init__(self, *args, **kwargs):
super(CodeShareForm, self).__init__(*args, **kwargs)
self.fields['code'].strip = False
class Meta:
model = Code
fields = ['title', 'code']
Models:
class Code(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True)
uid = models.UUIDField(max_length=255)
views = models.PositiveSmallIntegerField(default=0, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=30, null=True, blank=True)
code = NonStrippingTextField()
Fields:
from django.db.models import TextField
class NonStrippingTextField(TextField):
def formfield(self, **kwargs):
kwargs['strip'] = False
return super(NonStrippingTextField, self).formfield(**kwargs)