这是我的新知识,仍在学习中。
对我来说,现在的任务是使用mongodb创建搜索栏
我到目前为止所做的事情>
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()
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)
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'),
]
<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> </form> </nav> </html>
问题是我正在从mongodb获取数据。我想我很近。一旦我在搜索中输入值并点击Submit,它就会在下面出现错误。
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获取数据并在搜索中显示它。
再次感谢。
这是我的新知识,仍在学习中。现在我的任务是使用mongodb创建搜索栏到目前为止,我已经完成了创建mongodb_connection.py的工作,以使用pymongo从...