0

我只是想在我的 Django 应用程序中实现一个搜索视图。但是当我尝试在我的应用程序上搜索某些内容时,我收到以下错误:

'unrecognized token: "@"'

最后,我希望我的查询是类别和搜索词的组合。以便用户可以过滤特定类别(就像 Amazon.com 搜索字段一样),例如:http://127.0.0.1:8000/search/?category=1&q=hallo

base.html

...
   <div class="globalsearch">
            <form id="searchform" action="{% url 'search' %}" method="get" accept-charset="utf-8">
                <label for="{{ categorysearch_form.category.id_for_label }}">In category: </label> {{ categorysearch_form.category }}
                <input class="searchfield" id="searchbox" name="q" type="text" placeholder="Search for ...">
                <button class="searchbutton" type="submit">
                    <i class="fa fa-search"></i>
                </button>
            </form>
        </div>
    </div>

...

categorysearch_form 是一个下拉选择器,它从数据库中获取他的 ID。

视图.py

...
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
from django.views.generic import ListView


class globalsearch(ListView):
    """
    Display a Post List page filtered by the search query.
    """
    model = Post
    paginate_by = 10

    def get_queryset(self):
        qs = Post.objects.all()

        keywords = self.request.GET.get('q')
        if keywords:
            query = SearchQuery(keywords)
            title_vector = SearchVector('title', weight='A')
            content_vector = SearchVector('content', weight='B')
            tag_vector = SearchVector('tag', weight='C')
            vectors = title_vector + content_vector + tag_vector
            qs = qs.annotate(search=vectors).filter(search=query)
            qs = qs.annotate(rank=SearchRank(vectors, query)).order_by('-rank')

        return qs

...

网址.py

...

url(r'^search/$', views.globalsearch.as_view(), name='search'),

...

Search.html结果显示在这里:

{% extends 'quickblog/base.html' %}

{% block content %}
    {% for post in object_list %}
        <div class="post">
            <h1><u><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></u></h1>
            <p>{{ post.content|linebreaksbr }}</p>
            <div class="date">
                <a>Published by: {{ post.author }}</a><br>
                <a>Published at: {{ post.published_date }}</a><br>
                <a>Category: {{ post.category }}</a><br>
                <a>Tag(s): {{ post.tag }}</a>
            </div>
        </div>
    {% endfor %}

后模型

...
#Post Model
class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=75)
    content = models.TextField(max_length=10000)
    tag = models.CharField(max_length=50, blank=True)
    category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
    postattachment = fields.FileField(upload_to='postattachment/%Y/%m/%d/', blank=True ,null=True)
    postcover = fields.ImageField(upload_to='postcover/%Y/%m/%d/', null=True, dependencies=[
        FileDependency(processor=ImageProcessor(
            format='JPEG', scale={'max_width': 200, 'max_height': 200}))
    ])
    created_date = models.DateField(auto_now_add=True)
    published_date = models.DateField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    class Meta:
        ordering = ["-title"]

    def __str__(self):
        return self.title
...

我想我当时只是错过了一些东西......我已经读到这个问题来自本地 SQL 数据库。SQLite,这是真的吗?小号 有解决办法吗?谢谢

4

1 回答 1

0

在与朋友进行了一些修改后,我找到了解决方案,根据矢量搜索的 Django 文档确实不是那么好...

视图.py

class globalsearch(ListView):
    """
    Display a Post List page filtered by the search query.
    """
    model = Post
    paginate_by = 10

    template_name = 'quickblog/search.html'

    def get_queryset(self):
        keywords = self.request.GET.get('q')
        if keywords:
            query = SearchQuery(keywords)
            title_vector = SearchVector('title', weight='A')
            content_vector = SearchVector('content', weight='B')
            tag_vector = SearchVector('tag', weight='C')
            vectors = title_vector + content_vector + tag_vector
            qs = Post.objects.annotate(rank=SearchRank(vectors, query)).filter(rank__gte=0.1).order_by('-rank')
            return qs

模型.py

#Post Model
class Post(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=75)
    content = models.TextField(max_length=10000)
    tag = models.CharField(max_length=50, blank=True)
    category = models.ForeignKey(Category, verbose_name="Category", on_delete=models.CASCADE, null=True)
    postattachment = fields.FileField(upload_to='postattachment/%Y/%m/%d/', blank=True ,null=True)
    postcover = fields.ImageField(upload_to='postcover/%Y/%m/%d/', null=True, dependencies=[
        FileDependency(processor=ImageProcessor(
            format='JPEG', scale={'max_width': 200, 'max_height': 200}))
    ])
    created_date = models.DateField(auto_now_add=True)
    published_date = models.DateField(blank=True, null=True)

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    class Meta:
        ordering = ["-title"]

    def __str__(self):
        return self.title

搜索.html

{% extends 'quickblog/base.html' %}

{% block content %}
    {% for post in object_list %}
        <div class="post">
            <h1><u><a href="{% url 'post_detail' pk=post.pk %}">{{ post.title }}</a></u></h1>
            <p>{{ post.content|linebreaksbr }}</p>
            <div class="date">
                <a>Published by: {{ post.author }}</a><br>
                <a>Published at: {{ post.published_date }}</a><br>
                <a>Category: {{ post.category }}</a><br>
                <a>Tag(s): {{ post.tag }}</a>
            </div>
        </div>
    {% endfor %}


{% if is_paginated %}
    <ul class="pagination">
        {% if page_obj.has_previous %}
            <li><a href="?page={{ page_obj.previous_page_number }}">&laquo;</a></li>
        {% else %}
            <li class="disabled"><span>&laquo;</span></li>
        {% endif %}
        {% for i in paginator.page_range %}
            {% if page_obj.number == i %}
                <li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li>
            {% else %}
                <li><a href="?page={{ i }}">{{ i }}</a></li>
            {% endif %}
        {% endfor %}
        {% if page_obj.has_next %}
            <li><a href="?page={{ page_obj.next_page_number }}">&raquo;</a></li>
        {% else %}
            <li class="disabled"><span>&raquo;</span></li>
        {% endif %}
    </ul>
{% endif %}
{% endblock %}

网址.py

url(r'^search/$', views.globalsearch.as_view(), name='search'),

希望我能够帮助下一个帖子:D

于 2018-06-25T13:24:19.980 回答