Django前端搜寻

时间:2019-01-22 20:29:02

标签: python django postgresql user-interface heroku

当前使用Django 2.1,Python 3.6,PostgreSQL 11,并将数据库托管在Heroku上。

我想在已设置的设备视图上实施用户搜索。

Current Page Layout 该页面将列出我们数据库中的人员,并提供有关其详细信息的链接。我想要的是用户能够搜索特定人员并相应地填充列表。

Django文档提供了一些实现此步骤的步骤,但对于行动计划尚不完全清楚。

感谢您的帮助!

编辑:供参考,数据库中将有数百人,并且该数据库当前托管在Heroku上。

编辑:Models.py

class Topic(models.Model):
top_name = models.CharField(max_length=264,unique=True)
def __str__ (self):
    return self.top_name

class WebPage(models.Model):
    topic = models.ForeignKey('Topic',on_delete=models.PROTECT)
    name = models.CharField(max_length=264,unique=True)
    url = models.URLField(unique=True)
    def __str__(self):
        return self.name

class AccessRecord(models.Model):
    name = models.ForeignKey('WebPage',on_delete=models.PROTECT)
    date = models.DateField()

    def __str__(self):
        return str(self.date)

编辑: `#搜索代码

def search(request):

    keywords=''

    if request.method=='POST': # form was submitted

        keywords = request.POST.get("keywords", "")
        all_queries = None
        search_fields = ('name__name','date') # change accordingly
        for keyword in keywords.split(' '): 
            keyword_query = None
            for field in search_fields:
                each_query = Q(**{field + '__icontains': keyword})
                if not keyword_query:
                    keyword_query = each_query
                else:
                    keyword_query = keyword_query | each_query
                    if not all_queries:
                        all_queries = keyword_query
                    else:
                        all_queries = all_queries & keyword_query

        accesses = AccessRecord.objects.filter(all_queries).distinct()
        context = {'accesses':accesses}
        return render(request, 'proj_app/search.html', context)

    else: # no data submitted

        context = {}
        return render(request, 'proj_app/index.html', context)`

1 个答案:

答案 0 :(得分:1)

您可以使用此示例在自己的视图中实现自己的搜索引擎

def search(request):

    keywords=''

    if request.method=='POST': # form was submitted

        keywords = request.POST.get("keywords", "") # <input type="text" name="keywords">
        all_queries = None
        search_fields = ('title','content','resume') # change accordingly
        for keyword in keywords.split(' '): # keywords are splitted into words (eg: john science library)
            keyword_query = None
            for field in search_fields:
                each_query = Q(**{field + '__icontains': keyword})
                if not keyword_query:
                    keyword_query = each_query
                else:
                    keyword_query = keyword_query | each_query
                    if not all_queries:
                        all_queries = keyword_query
                    else:
                        all_queries = all_queries & keyword_query

        articles = Article.objects.filter(all_queries).distinct()
        context = {'articles':articles}
        return render(request, 'search.html', context)

    else: # no data submitted

        context = {}
        return render(request, 'index.html', context) 

您只需更改以下内容:

1-html中使用的name属性

<input type="text" name="keywords">
keywords = request.POST.get("keywords", "")

2-模型类的名称

3-该模型的搜索字段

然后在您的search.html模板中

<table>
    <tr>
        <td>Title</td>
        <td>Author </td>
    </tr>
    {% for article in articles %}
    <tr>
        <td>{{article.title}}</td>
        <td>{{article.author}}</td>
    </tr>
    {% endfor %}
</table>