【发布时间】:2018-06-20 21:45:08
【问题描述】:
我只是想在我的 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 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,这是真的吗? 小号有解决办法吗? 谢谢
【问题讨论】:
-
我认为,将数据库更改为
postgres将解决问题。你试过了吗? -
你为什么要首先在 sqlite 中使用 postgres 函数?
-
我不支持 Django,我刚刚在另一个线程上发现了这个问题。直到现在我还没有尝试过 postgres,但我会在接下来的几天里尝试。我在网上的某个地方找到了这个代码示例,但我记得那个 smb。写了一些关于 Postgres 的文章。无论如何,感谢您的反馈 :) 喜欢这个社区
-
我也可以使用 MySQL 还是这个 PostGres 只是相关的查询语句?
-
我现在已经在我的机器上设置了一个 PostGres DB(好东西),现在我没有返回任何错误,现在我什么也没有返回......是的......我在在我的views.py中以调试模式(Pycharm)“返回qs”。请在此处查看结果:pastebin.com/zJ3TVQd1 Any Idea?
标签: django search django-queryset operationalerror