2010-07-01 27 views
13

有没有办法让django-haystack的{% highlight %}模板标签显示传入的完整变量,而不是在第一次匹配之前删除所有内容?django干草堆高亮模板标签问题

我使用的是这样的:

{% highlight thread.title with request.GET.q %} 
+0

5年后,我也有同样的问题。 Github上甚至有一个问题:https://github.com/django-haystack/django-haystack/issues/748 – weeheavy 2015-07-25 16:10:04

回答

9

我从未使用过干草堆,而是从快速浏览一下在the docsthe source看起来你可以让自己的自定义荧光笔,并告诉草垛使用这不是

from haystack.utils import Highlighter 
from django.utils.html import strip_tags 

class MyHighlighter(Highlighter): 
    def highlight(self, text_block): 
     self.text_block = strip_tags(text_block) 
     highlight_locations = self.find_highlightable_words() 
     start_offset, end_offset = self.find_window(highlight_locations) 

     # this is my only edit here, but you'll have to experiment 
     start_offset = 0 
     return self.render_html(highlight_locations, start_offset, end_offset) 

,然后设置

HAYSTACK_CUSTOM_HIGHLIGHTER = 'path.to.your.highligher.MyHighlighter' 

在你的settings.py

2

通过@second作品的答案,但是如果你也不想它切断字符串的结尾,你是最大长度下,你可以试试这个。仍然测试它,但它似乎工作:

class MyHighlighter(Highlighter): 
    """ 
    Custom highlighter 
    """ 
    def highlight(self, text_block): 
     self.text_block = strip_tags(text_block) 
     highlight_locations = self.find_highlightable_words() 
     start_offset, end_offset = self.find_window(highlight_locations) 
     text_len = len(self.text_block) 

     if text_len <= self.max_length: 
      start_offset = 0 
     elif (text_len - 1 - start_offset) <= self.max_length: 
      end_offset = text_len 
      start_offset = end_offset - self.max_length 

     if start_offset < 0: 
      start_offset = 0 
     return self.render_html(highlight_locations, start_offset, end_offset)