Y
Y
YraganTron2017-02-03 03:20:40
Django
YraganTron, 2017-02-03 03:20:40

How to display data from the database, where there is html markup, in html (Django)?

I have a form through which users submit messages. To send a message to a specific user in a post, a definition is inserted into the form. text, which later in the view will be wrapped with the tags I need with the attributes I need (hereinafter, all this is used in js). At first I used safe, but after thinking a little, I realized that in this way users can draw whatever they want. I need to draw specifically my tags, and leave everything else behind, if it suddenly exists. I understand that you need to write your own filter, but I don’t fully understand what exactly it should do.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aleksandr R, 2017-02-03
@YraganTron

First, you cut out everything unnecessary from the message, then you add the tags you need with a filter.
Here is an example of how to work with text using filters:

from django import template
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe

register = template.Library()

@register.filter(needs_autoescape=True)
def initial_letter_filter(text, autoescape=True):
    first, other = text[0], text[1:]
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    result = '<strong>%s</strong>%s' % (esc(first), esc(other))
    return mark_safe(result)

More about filters: djbook.ru/rel1.9/howto/custom-template-tags.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question