I have Django´s Blog APP installed, all working fine, but I need to add posts (via admin) with HTML in the post content field, now, the text area can only read plain text (it doesn´t render HTML).
This is the field:
(models.py)
content = models.TextField()
This is the HTML for this field:
<h6 class="card-text" ><small>{{post.content|slice:":500" |linebreaks |safe}}</small></h6>
Question is: are there special configs for Django/Python in order for the field to render HTML?
The safe tag should already do that... have you tried this:
{% autoescape off %}
{{ post.content }}
{% endautoescape %}
https://code.djangoproject.com/wiki/AutoEscaping
Hope this helps!
Related
So I want to make a blog post with HTML formatting from the admin page directly. For example from the models.py, you see the description is a TextField. When I go to the admin page to make a blog post, I want to be able to add HTML tags to my blog. So the text field will have HTML content. While I call the blog post onto the HTML template I want it to read the description as a HTML file and not just a text field.
models.py
from django.db import models
class Blog(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
date = models.DateField()
Blog.description at admin page
<HTML>
<body>
<p>Welcome to my first blog post!!</p>
</body>
</html>
blog.html
<h1>{{ blog.title }}</h1>
<hr>
<p>{{ blog.description }}</p>
Any help is appreciated. Thanks!
You can render it with the |safe template filter [Django-doc]. This will disable escaping HTML fragments, and it will thus no longer convert < to < for example:
<h1>{{ blog.title|safe }}</h1>
<hr>
<p>{{ blog.description|safe }}</p>
You however might want to take a look at the django-ckeditor package [GitHub] which offers a dedicated field, widget, etc. to enable editing the text with respect to rendering.
I am drafting an email template to users when they successfully updated their passwords.
I used {{ autoescape off }} in the template, which is rendered by using render_to_string().
However, the email content shows the HTML angle brackets directly like this:
Hi <span style='color:blue'>user! </span>
Your password is updated successfully!
I am using Django2.0 and my code looks like this:
views.py
from django.core.mail import send_mail
from django.template.loader import render_to_string
def sendmail(request, title)
email_title = title
email_content = render_to_string('template.html',{'username':request.user.username})
recipient = request.user.email
send_mail(
email_title,
email_content,
'myemail#email.com',
[recipient,],
)
template.html
{{ autoescape off}}
Hi <span style='color:blue'>user! </span>
Your password is updated successfully!
{{ endautoescape }}
Is there anything wrong with my code?
Otherwise, is autoescape always on while using render_to_string()?
This has nothing to do with autoescape, which is for rendering variables (it is also a template tag, so you use it with {% autoescape off %}, not {{ autoescape off }}). It is doing nothing at all in your current template.
Your issue is that you're trying to put HTML into the plain text body of an email.
send_mail expects a plain text message body. If you want to have a HTML body then you need to supply a html_message argument:
send_mail(
email_title,
'', # Empty plain text body - not recommended and you should supply a plain text alternative
'myemail#email.com',
[recipient,],
html_message=email_content, # This will render as HTML
)
I am using django to create a blog. In my model I am using a text field for my blog content. But I am unable to insert any image or a clickable link. How to add links(clickable) and insert images?
{% block body %}
{% for blog in blog %}
<h1>{{blog.title}}</h1>
{{blog.text|safe|linebreaks}}
<br>
<h6>{{blog.user}}</h6>
<br>
<br>
{% endfor %}
{% endblock %}
there is no thing like clickable link in Database.you can put link as text and while importing it to your HTML use , and for Images there are two options. either you put your image path name in database or change TextField in models to FileField.
The problem is that I want to show the content of a Post in the template but I don't know how
The model of Post is:
from ckeditor.fields import RichTextField
class Post(models.Model):
...
content = RichTextField(verbose_name='contenido')
...
And in the template I have a for to show all the post that is like this:
{% for post in posts %}
...
{{ post.content }}
...
{% endfor %}
But when I see the page in the browser shows this:
< p > Post content < /p >
Instead of this:
Post content
You need to mark the content as safe. So change your template to:
{% for post in posts %}
...
{{ post.content|safe }}
...
{% endfor %}
By default HTML is not escaped and so is displayed as text, which is why you're seeing the <p> tags. You need to mark the field as safe so that Django renders it as HTML. See the documentation for more info.
{{article.body|safe}}
or
{{article.body|truncatechars:150|safe}}
or
{{article.body|truncatewords:25|safe}}
i have problem with results in django-cms and haystack search. I'm using django-cms-search plugin, haystack as backend. Haystack returns correct results. But i want to show "teaser" in search results.
I can access absolute URL and title of page via template this way:
{% for result in page.object_list %}
<div class="searchResults">
<h2>{{ result.object.get_title }}</h2>
{{ result.object.placeholders.all }}
<p>{% blocktrans %} Read more {% endblocktrans %}</p>
Problematic part is {{ result.object.placeholders.all }}. I have on every page content in placeholder with name content.
{{ result.object.placeholders.all }} returns only name of the placeholders.
Search results should look like this:
PAGE TITLE
PAGE TEASER
READ MORE LINK
In teaser there should be first 50 words from search-matched page.
Is this possible to access placeholder content from template?
Thank you for your tips.
Haystack has templatetag higlight which creates "teaser" as i requested.
Template code can look like this:
{{ result.object.get_title }}
{% highlight result.text with request.GET.q max_lenght 40 %}
{{ result.object.get_absolute_url }}
Thanks to guys from #haystack IRC channel.