I have a field in database description that system will save HTML code in it.

and I have a search system that works with Q:

Post.objects.filter(Q(name__icontains=keyword) | Q(description__icontain=keyword))

It works fine but the problem refers to it when user searchs for example '<strong>' or 'strong' it will returns the rows that have '<strong>' word in them but it shouldn't consider the HTML tags.

So how to search a value in HTML content with Django ORM that don't consider HTML tags?

有帮助吗?

解决方案 3

I think it's a good action:

from django.utils.html import strip_tags
rows = Post.objects.filter(Q(name__icontains=keyword) | Q(description__icontain=keyword))
if rows:
    for j,i in enumerate(rows):
        if keyword not in strip_tags(i.name) and keyword not in strip_tags(i.description):
            del rows[j]
return render(request,'posts.html',{'rows':rows})
  1. Fetching data from db with filter.
  2. Strip tags the results and then filtering them again.

其他提示

I'd probably add a second field called stripped_description and use django's striptags filter to strip out html tags, and have django search on that field. It should still find the row you need to recall the actual description field containing the HTML code, should you need to display that as a result, but that's the only means I've used to "ignore" html tags.

You can or probably should look into a proper search function using haystack, my favorite search engine to use with it is whoosh (pip install whoosh) if you are not doing hardcore search functions. You can define your content to be indexed like this:

{{ object.title }}
{{ object.description|strip_tags }}

It's fairly easy to setup, and once you have done it, setting up for the next project would be in minutes.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top