简体   繁体   中英

Django custom filter

I'm new to django and I have a plain text inserted from admin and I wanted to user custom filter to replace linebreak in plain text into new line . I'm unable to see the changes of filter in browser

templatetags/filter.py

register = template.Library()

@register.filter(name='replace_linebr')
def replace_linebr(value):

    return str(value).replace('
','   ')

Following filter works but I believe replace is not working

from urllib import quote_plus

register = template.Library()

@register.filter(name='replace_linebr')
def replace_linebr(value):

    return quote_plus(value)

my templates

{% extends 'base.html' %}
{% load staticfiles %}
{% load filters %}

{% block blog_post %}
    <div class="blog-post">

        <p class="blog-post-title">{{ posts.title }}</p>
        <p class="blog-post-meta"><i class="fa fa-clock-o">&nbsp {{ posts.pub_date|date  }}</i> </p>
        {{ posts.description|replace_linebr }}

    </div><!-- /.blog-post -->
{% endblock %}

My folder structure

在此处输入图片说明

settings.py 在此处输入图片说明

Any help is appreciated. Thanks in advance

Add blog in INSTALLED_APPS and it should work.

Update

You have typo, your python file is filter.py and in template you are doing {% load filters %} .

{% load filters %} should be {% load filter %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM