django搜索页面抛出数据 [英] django search page throwing data

查看:32
本文介绍了django搜索页面抛出数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使搜索页面正常工作方面,我面临一些困难.问题是我的模板和视图在搜索页面上抛出数据.而且我的搜索按钮功能不起作用.如果有人可以看看并提出建议,我将不胜感激.

I am facing bit of difficulty in making search page work. The problem is my template and view throwing data on search page. And my search button function doesnt works. If some one can have a look and advise what i m missing would be highly appreciated.

谢谢.

这是我的搜索页面看起来多么难看

this is how ugly my search page looks

  • 以下是我的观点:
from django.shortcuts import render

# Create your views here.
#from django.shortcuts import render
from django.http import HttpResponse
from .mongodb_connection import mongosearch
from .models import AppModel
from django.db.models import Q


# Create your views here.

def search_view(request):

    model = AppModel
    template_name = 'search.html'
    results = []
    title_term = ""
    desc_term = ""
    search_term = ""
    url_term = ""
    titles = AppModel.objects.all()
    url = AppModel.objects.all()

    if 'search' in request.GET:
         search_term = request.GET['search']
         titles = titles.filter(
                 Q(title__icontains=search_term) |
                 Q(desc__icontains=search_term)
         )
         titles = AppModel.objects.all()
         results = mongosearch(title=title_term
                                #,desc = desc_term)
                               )
         print(results)

    context={
        'results':results,
    'search_term':search_term,
        'titles':titles
    }


    return render(request, 'search.html', context)

  • 跟随html页面
  • <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    
    <!doctype html>
    <html>
    <nav class="navbar navbar-light bg-light">
        <form class = "form-inline my-2  my-lg-1" method = "GET">
        <!--action = "{%url 'search_view'%"> -->
            <input
                    class="form-control mr-sm-2"
                    type="search"
                    placeholder="Search"
                    aria-label="Search"
                    name = 'search'
                    value = "{{request.GET.search}}">
            <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
    
        </form>
        <br><br>
        {% if titles %}
            <ul class="messages">
                {%for i in titles %}
                    <li class = "list-group-item">
                        {{i.title}}
                    </li>
                {% endfor %}
            </ul>
        {% endif %}
    
    </nav>
    
    </html>
    
    

    • mongo_connection
    • from pymongo import MongoClient
      from pprint import pprint
      
      def mongosearch(title=""):
          connection = MongoClient('localhost',27017)
          db = connection.djangodb
          collection = db.spiderCollection
          titles = collection.find()
          for title in titles:
              pprint(titles)
      

      推荐答案

      看起来您正在用视图中的所有对象覆盖已过滤的查询集.您需要删除多余的行:

      Looks like you are overwrite filtered queryset with all objects in your view. You need to remove redundant line:

           search_term = request.GET['search']
           titles = titles.filter(
                   Q(title__icontains=search_term) |
                   Q(desc__icontains=search_term)
           )
           titles = AppModel.objects.all()  # remove this line to not overwrite list
           results = mongosearch(title=title_term
                                  #,desc = desc_term)
                                 )
           print(results)
      

      这篇关于django搜索页面抛出数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆