Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django display textfields

I have a form with textfields in Django, and users enter texts line by line. When I look at the entries in admin side, I saw exactly how user wrote it but when I display it on my website, in Django templates, it just joins the sentences and I couldn't display the text line by line anymore.

To illustrate, user enters text as

-bla

-bla1

-bla2

However, it looks like in my page as

-bla1-bla2-bla3

This is my code in the template:

<div>
   <p>
      {{ rev.myTextField }}
   </p>
</div>
like image 920
brsbilgic Avatar asked Nov 20 '11 20:11

brsbilgic


People also ask

What is Max_length in Django?

The maximum length (in characters) of the field. The max_length is enforced at the database level and in Django's validation using MaxLengthValidator . If you are writing an application that must be portable to multiple database backends, you should be aware that there are restrictions on max_length for some backends.

When should one use the include () in Django?

The include tag allows you include a template inside the current template. This is useful when you have a block of content that are the same for many pages.

What is text field in Django?

TextField is a large text field for large-sized text. TextField is generally used for storing paragraphs and all other text data. The default form widget for this field is TextArea.


2 Answers

In HTML, newlines between text don't imply a newline in display; you need to use HTML to insert the newline. The most straightforward way is with the <br /> tag, although there are other methods as well.

Django has a template filter for this: use |linebreaks. Here's the Documentation.

So, change:

{{ rev.myTextField }}

to:

{{ rev.myTextField|linebreaks }}
like image 179
Luke Sneeringer Avatar answered Oct 14 '22 23:10

Luke Sneeringer


Try "safe" tag

{{ rev.myTextField|safe }}
like image 44
Denis Kabalkin Avatar answered Oct 14 '22 23:10

Denis Kabalkin