How do I display db text/string fields in the format in which they were entered on the form?

243 views
Skip to first unread message

Steven Vannoy

unread,
Oct 20, 2016, 12:13:56 PM10/20/16
to web2py-users
I have an app in which users can fill out a form and then that form is displayed in a view. The form has many fields, several of which are free form text boxes, where type is 'text'. Specifically my db table is called 'sites' and a field related to my question is 'other', in which a user might enter a long description of information not captured in any of the fields of the form. Currently, to display the field in my view, I am just using:

{{if len(site.other) > 0:}}
    <h4> Miscellaneous "Other"</h4>
        <p>{{=site.other}}</p>
{{pass}}

The text is being displayed in one long string. New lines (returns) that the user entered in the form are not reflected in the output. I don't know much about html, but I do know that in general it ignores new lines (you have to use a tag like <br> to get a new line), but how can I display the text the user entered into the form and have the new lines displayed? Some users have also done indentation to display a list of items with asterisks for bullets, so preferably that would render also. An example might be:

We expect applicants to do the following
  * show up on time
  * dress professionally
  * complete all tasks in a timely fashion


I am currently not using a CSS style sheet, but I suspect that might be part of the solution. Thus far, my view just has the default:

{{extend 'layout.html'}}

at the top and then my fields.

Basically I want the functionality that this form that I'm filling out right now has, my text will be rendered with new lines and indentations just as I've typed them into this text box (note I don't need the fancy code formating and all of the cool things I can do in this form). 

Thanks

Dave S

unread,
Oct 20, 2016, 2:01:09 PM10/20/16
to web...@googlegroups.com


On Thursday, October 20, 2016 at 9:13:56 AM UTC-7, Steven Vannoy wrote:
I have an app in which users can fill out a form and then that form is displayed in a view. The form has many fields, several of which are free form text boxes, where type is 'text'. Specifically my db table is called 'sites' and a field related to my question is 'other', in which a user might enter a long description of information not captured in any of the fields of the form. [...]
The text is being displayed in one long string. New lines (returns) that the user entered in the form are not reflected in the output. I don't know much about html, but I do know that in general it ignores new lines (you have to use a tag like <br> to get a new line), but how can I display the text the user entered into the form and have the new lines displayed? Some users have also done indentation to display a list of items with asterisks for bullets, so preferably that would render also. [...]

I'm going to go out on a limb, and opine that you never even get the newlines or indentation (but do get the asterisks), all that having been stripped out by the textarea input control.  You need something that actually makes it to you and that you can recognize as format control.  The easiest is probably to use web2py's wiki support:
<URL:http://web2py.com/books/default/chapter/29/03/overview#MARKMIN-basics>

(BTW, under the covers, you are using CSS style sheets ... either Bootstrap3 or stupid.css, depending on which layout.html you started with;
adding a custom style sheet probably isn't necessary for this task.)

/dps


Anthony

unread,
Oct 20, 2016, 2:05:20 PM10/20/16
to web2py-users
You could use a <pre> tag: http://www.w3schools.com/tags/tag_pre.asp. You might need some CSS to get the font/styling you like.

You could also replace the newlines with <br> tags:

{{=XML(site.other.replace('\n', '<br>'), sanitize=True, permitted_tags=['br/'])}}

Because the text now contains <br> HTML tags, it is necessary to wrap it in XML() to prevent web2py from escaping the HTML -- but you also want to sanitize the text and allow only <br> tags to prevent malicious code from being executed.

Anthony

Anthony

unread,
Oct 20, 2016, 2:07:15 PM10/20/16
to web2py-users
On Thursday, October 20, 2016 at 2:01:09 PM UTC-4, Dave S wrote:

I'm going to go out on a limb, and opine that you never even get the newlines or indentation (but do get the asterisks), all that having been stripped out by the textarea input control.

No, a textarea does preserve newlines.

Anthony

Steven Vannoy

unread,
Oct 20, 2016, 2:29:51 PM10/20/16
to web...@googlegroups.com
Thanks, the replace option from @Anthony worked pretty well, it got the new lines but didn't provide indentation (as expected). The <pre></pre> tags get both, and I like the font well enough except that it is a lighter gray on a gray background so if I can manage to get that changed I'll probably go with that option.

Yes Dave, you were right it wasn't stripping the asterisks but wasn't rendering them the way I would like. 

Steven


--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/WM3hhsJVUYU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Oct 20, 2016, 3:07:57 PM10/20/16
to web2py-users
Oops, forgot about the indenting -- just replace all spaces with "&nbsp;":

{{=XML(site.other.replace('\n', '<br>').replace(' ', '&nbsp;'),
       sanitize
=True, permitted_tags=['br/'])}}

Anthony

Patito Feo

unread,
Aug 31, 2019, 1:20:47 PM8/31/19
to web2py-users
Hi,

Im testing this, but it seems somethings changed to date. 

Here is what im getting on the textarea:

prueba textarea 3<br />prueba textarea 3<br />prueba textarea 3


As you can see, \n new lines are converted to <br> but render as <br />.


Any suggestion?


Regards,

To unsubscribe from this group and all its topics, send an email to web...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages