Django Front End Search

六眼飞鱼酱① 提交于 2020-01-06 08:46:31

问题


Currently using Django 2.1,Python 3.6, PostgreSQL 11, and hosting the DB on Heroku.

I would like to implement a user search on a device view that I have setup.

Current Page Layout This page will list people in our DB and will provide a link to details about them. What I would like is for a user to be able to search for a particular person and have the list populate accordingly.

Django documentation provides some steps to implement this but is not entirely clear about an action plan.

Thanks for any help!

EDIT: For reference there will be hundreds of people in our DB and it is currently hosted on Heroku.

EDIT: 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)

EDIT: `# Search Code

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:


You can use this example to implement your own search engine in your views

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) 

You just have to change the following :

1 - The name attribute used in html

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

2 - The name of the model class

3 - The search fields of that model

Then in your search.html template

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


来源:https://stackoverflow.com/questions/54315986/django-front-end-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!