newline in models.TextField() not rendered in template - django

I have a model with an attribute,
desc = models.TextField()
I entered data using admin interface provided by Django and then later viewed my template where the database values are fetched and displayed.
In my admin interface I left newline (just by leaving blank lines in between my paragraphs) but they are displayed as a single paragraph in my template.
I'm using Django 1.3 and MySQL.

linebreaks
Replaces line breaks in plain text with appropriate HTML; a single newline becomes an HTML line break (<br />) and a new line followed by a blank line becomes a paragraph break (</p>).
For example:
{{ value|linebreaks }}

Just a note, I was having a similar problem with newlines not showing up and I realized that when a TextField is declared as readonly, the text is wrapped with HTML paragraph tags:
<p> text </p>
as opposed to pre tags:
<pre> text </pre>
Pre tags preserve new line spaces, so if you do NOT make the field readonly, you will see the linespaces.

John, looks like you got a great answer from Ignacio. I just wanted to point out the steps that I took to use Ignacio's answer for those who may be confused (like I was). Inside my template where I display the text field I added the "|linebreaks" behind the template field name (in my case a "job.desc"):
<ul>
{% for job in varDataObject %}
<li>
<h4>
<a href="#" onclick='funPath("Job", {{ job.id }})'>{{ job.title }} </a>
</h4>
Description: {{ job.desc|linebreaks }}

Saving text with newline (not \n) in your model's TextField() using sql:
update event
set description='xyz
asdasd
oaisjd '
where id=1;
In Django template:
{{ object.description|linebreaks }}

Related

Error return truncatechars & safe by using Built-in template tags and filters

I try to Truncates the string and remove the html tags,
First, when I write it this way.
{{ post.context|safe }}
or
{{ post.context |truncatechars:100 }}
The left navigation bar shows normal.
But when I write this, this part of the HTML is gone.
{{ post.context |truncatechars:100|safe }}
But I can still find this Html in the source code.
So what can I do to get the correct results?thank you
If you just want to safely show content with HTML formatting.
{{ post.context|safe }}
If you truncate then some HTML tags may not get closed tag and you will get an irregular view.
If you want to strip HTML tags, you can strip by striptags and truncate characters using slice filters.
{{post.context|striptags|slice:':300'}}
Although it's kinda late to answer if you want to show the safe code with tags then use {{ post.context|truncatewords_html:30|safe }} or {{ post.content|truncatechars_html:100|safe }}. This won't break your code and will display your desired content.
No need to struggle around. You need to combine truncate, safe, and striptags
Respect the order below:
{{ string_variable|striptags|safe|truncate(100) }}
Does that help you?

how can I add more character space to the home page tagline in big cartel?

Does anyone know how to edit the CSS in a Big Cartel theme (specifically the FOUNDRY theme) to allow for more text in the home page tagline? Currently, you can only type about 100 characters. I would like to be able to write a bit more (like a small paragraph about the shop and current product overview).
The text box is limited to 100 characters, so to edit that you'll need to change the code the Advanced > Home section of the Customize Design area. Find this code at the top:
{% if theme.home_tagline != blank %}
<div class="home_tagline">
{{ theme.home_tagline }}
</div>
{% endif %}
And replace the {{ theme.home_tagline }} with your own text.

Bootstrap popover only shows one word of string

I have a set of popovers that show supplemental info upon hovering. They display, but only the first word of the string.
template:
<a data-toggle="popover" data-trigger="hover" data-content={{std.Description}} > <i class="fa fa-question"></i></a>
...
$("[data-toggle=popover]").popover();
The field in question is a Django TextField:
Description = models.TextField(blank=True)
Behavior:
This description had several words, but all after the first were cut off.
The solution is the same as for Contain form within a bootstrap popover? , though the title of the post may be misleading. The issue is that there need to be quotes containing the desired variable:
data-content="{{std.Description}}"
Additionally, if you want to include html in the popover (lists, <br>, etc.), include data-html="true".

How can I truncate a text and use the second part of the text?

I'm researching for a little problem.
I get on my Django template a text. The point is truncate that text, and add the second part of the text in other tag.
I read about:
{{ value|truncatechars_html:x }}
It can works, but I can't use the second part of truncated text.
If someone has an idea...Thanks!!!!
Use slice - see docs here. Strings in python are also lists of characters, so you can do something like this:
First part is {{ value|slice:":x" }}
Second part is {{ value|slice:"x+1:" }}
HOWEVER, if you need to keep some html code, slice won't do it. For that, you will need to write your own custom tag (you can look into the code from truncate chars) which returns two values - one for each part of your string.
Assuming your filter returns something like this:
def your_truncate_filter(value, arg):
... # your code for splitting
data = {}
data['first'] = "....." # first part of string with html tags !!!
data['second'] = "....." # second part of string
return data
you can access it in your template like this:
{% with parts=value|your_truncate_filter:x %}
{{ parts.first }}
{{ parts.second }}
{% endwith %}

Can a django template be referenced from within a loop on another template?

I have a django template which loops over many notes/comments. As a simplified example take this.
{% for note in notes %}
<p>
Date added: {{ note.date_added }}
{{ note.note|urlize|url_target_blank|linebreaks }}
</p>
{% endfor %}
Then on the same page I have a form to add a new note. This note form is an ajax form and returns the newly submitted note back to the page and appends it at the end of the already existent note area.
I don't like this because I have to maintain the same html structure both in the page for the initial load, as well as in the response from the ajax form.
Is there a way to put a call to another template, inside of a template (in this for loop) so I can maintain the note formatting in one location only?
Thanks.
Perhaps you're looking for the "include" tag: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#include