【问题标题】:Custom template filter: replace HTML element with regex filtered text (Django 2.1)自定义模板过滤器:用正则表达式过滤文本替换 HTML 元素(Django 2.1)
【发布时间】:2019-06-27 02:56:02
【问题描述】:

我正在构建一个搜索引擎,它需要一个自定义过滤器来显示关键字周围的文本,例如 Google 结果页面上的摘录。我正在使用正则表达式来识别周围的单词。这是我的过滤器代码:

@register.filter(needs_autoescape=True)
@stringfilter
def show_excerpt (value, search_term, autoescape=True):
    # make the keyword put into the search engine case insensitive #
    keywords = re.compile(re.escape(search_term), re.IGNORECASE)
    # make excerpt return 300 characters before and after keyword #
    excerpt_text = '.{300}' + str(keywords) + '.{300}'
    # replace the original text with excerpt #
    excerpt = value.sub(excerpt_text, value)
    return mark_safe(excerpt)

view.py 中搜索引擎的代码:

def query_search(request):
    articles = cross_currents.objects.all()
    search_term = ''
    if 'keyword' in request.GET:
        search_term = request.GET['keyword']
        articles = articles.annotate(similarity=Greatest(TrigramSimilarity('Title', search_term), TrigramSimilarity('Content', search_term))).filter(similarity__gte=0.03).order_by('-similarity')
    context = {'articles': articles, 'search_term': search_term}
    return render(request, 'query_search.html', context)

HTML 模板(它包括一个自定义的highlight 过滤器,用于突出显示输入搜索引擎的关键字):

<ul>
{% for article in articles %}
<li><a href="{% url 'search:article_detail' article.ArticleID %}">{{ article|highlight:search_term }}</a></li>
<p> {{ article.Content|highlight:search_term|show_excerpt:search_term }} </p>
{% endfor %}
</ul>

错误消息:“SafeText”对象没有属性“sub”

我认为我做错了.sub。我只需要摘录来替换整个原始文本(我要过滤的文本)。原始文本从数据的开头开始,但我只想显示关键字周围的数据,我的highlight 自定义过滤器突出显示关键字(就像在 Google 上一样)。任何想法?

编辑:当我执行re.sub(excerpt_text, value) 时,我收到错误消息 sub() 缺少 1 个必需的位置参数:'string'。

【问题讨论】:

    标签: regex django


    【解决方案1】:

    我决定放弃正则表达式,只做旧的字符串切片。过滤器的工作代码:

    @register.filter(needs_autoescape=True)
    @stringfilter
    def show_excerpt(value, search_term, autoescape=True):
        #make data into string and lower#
        original_text = str(value)
        lower_original_text = original_text.lower()
        #make keyword into string and lower#
        keyword_string = str(search_term)
        lower_keyword_string = keyword_string.lower()
        #find the position of the keyword in the data#
        keyword_index = lower_original_text.find(lower_keyword_string)
        #Specify the begining and ending positions of the excerpt#
        start_index = keyword_index - 10
        end_index = keyword_index + 300
        #Define the position range of excerpt#
        excerpt = original_text[start_index:end_index]
        return mark_safe(excerpt)
    

    【讨论】:

      【解决方案2】:

      你需要调用 re.sub(),而不是 value.sub()。您在 SafeText 对象上调用 sub,.sub() 是一个正则表达式函数。

      我没有测试过你的代码,但是如果剩下的代码是正确的,你应该把那行改成re.sub(excerpt_text, value)

      【讨论】:

      • 我把它改成了那个,现在错误信息是 sub() missing 1 required positional argument: 'string'
      猜你喜欢
      • 2010-10-28
      • 1970-01-01
      • 1970-01-01
      • 2021-08-16
      • 2011-11-10
      • 1970-01-01
      • 2019-05-01
      • 2016-07-06
      • 2011-05-28
      相关资源
      最近更新 更多