【问题标题】:django pymongo search bar error need guidance / tutorialdjango pymongo 搜索栏报错 需要指导/教程
【发布时间】:2020-05-20 21:56:50
【问题描述】:

我对这一切都很陌生,还在学习。

现在我的任务是使用 mongodb 创建搜索栏

到目前为止我做了什么

  • 创建了 mongodb_connection.py 以使用 pymongo 建立连接
from pymongo import MongoClient


def mongosearch(title=""):
    connection = MongoClient('localhost',27017)
    db = connection.djangodb
    collection = db.spiderCollection
    title = collection.find()
    for title in titles:
        pprint.pprint()
  • 从 mongodb_connection 创建视图和导入方法如下:
from django.shortcuts import render
from django.http import HttpResponse
from .mongodb_connection import mongosearch
from .models import AppModel

# Create your views here.

def search_view(request):

    results = []
    title_term = ""
    search_term = ""
    titles = AppModel.objects.all()

    if 'search' in request.GET:
        search_term = request.GET['search']
        titles = titles.filter(title__icontains=search_term)
        results = mongosearch(title=title_term)
        print(results)

    context={
        'results':results,
        'count':len(results),
        'search_term':search_term,
        'titles':titles
    }

    return render(request, 'search.html', context)
  • 添加了models.py
from django.db import models

class AppModel(models.Model):
    title = models.CharField(max_length=100, primary_key=True)
    desc = models.CharField(max_length=100)
    url = models.CharField(max_length=100)

    class Meta:
        db_table = "spiderCollection"
  • 然后修改网址
from django.urls import path, include
from . import views
from django.conf.urls import url

app_name = 'searchapp'
urlpatterns=[
    path('',views.search_view, name='search_view'),

]
  • 在应用程序/模板下创建了 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">
        <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>
        <ul>
            {%for i in titles %}
            <li>
                {{i.titles}}
            </li>
            {% endfor %}
        </ul>
    </form>
</nav>

</html>

问题是我正在从 mongodb 获取数据。我想我很接近。一旦我在搜索中输入值并点击提交,它就会出现以下错误。

Exception Type: NameError
Exception Value:    
name 'titles' is not defined
Exception Location: /Users/qasimbutt/PycharmProjects/IntegratedProject/searchui/searchapp/mongodb_connection.py in mongosearch, line 9

有人可以帮助我,我可以在 mongodb_connection.py 文件中进行哪些更改以使其滚动?如果这里有什么遗漏,也请告知。 因为我需要从 mongodb 中获取数据并在搜索下显示。

再次感谢。

【问题讨论】:

    标签: python django search pymongo


    【解决方案1】:

    看看你的第一个函数,这行:

        for title in titles:
    

    在任何地方定义之前引用titles。您的意思可能是:

        titles = collection.find()
        for title in titles:
    

    【讨论】:

    • 感谢您的观察,我已修复该问题,但是当我运行它时,它会引发以下错误。有任何想法吗? #######异常类型:AttributeError异常值:'function'对象没有属性'pprint'异常位置:/Users/qasimbutt/PycharmProjects/IntegratedProject/searchui/searchapp/mongodb_connection.py in mongosearch, line 10跨度>
    • 我猜你想要pprint(title)
    • 嘿Belly Buster,感谢您的提示。它解决了一半的问题。但真的很有帮助。我无法显示结果,搜索现在似乎工作正常。但无法显示结果。如果您可以查看我的模板文件 .html,请再次感谢
    • 建议您确切地找出不工作的地方并开始另一个问题。
    • 嗨,Belly Buster,我取得了相当大的进步,但仍然存在轻微的 GUI 问题,我将其发布在这里 stackoverflow.com/questions/60135412/… 。提前感谢,你最好的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多