Django 无法识别的标记:查询时为“@”

Django unrecognized token: "@" while query

我只是想在我的 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。

views.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

...

urls.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 型号

...
#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 数据库 aka。 SQLite,这是真的吗? 某人。有解决办法吗? 谢谢

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

views.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

models.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

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 %}


{% 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 %}

urls.py

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

希望我能够帮助下一位 Post :D