我正在我的应用程序上实现某种搜索机制,试图从输入中获取查询,在数据库(sqlite)中查找并将其呈现为模板。问题是我遇到了错误:
.views没有返回HttpResponse对象。它返回None。
逻辑就像:
1)制作将检查给定查询的功能
def search_query(request,search,query):
checking = check_in(query) #check if query is in DB
if checking == False: #if not, get data from search api and save it
search_querys = requests.get(search)
json_search = search_querys.json()
for each in json_search['data']:
user_id = each['id']
name = each['name']
picture = each['picture']['data']['url']
Profiles.objects.create(user_id=user_id, name=name, picture=picture)
return render(request, 'FB_search/home.html')
else: # <--assuming that here's the problem. For testing purpose, I'm writing query for which I know that they are in DB and I'd like to return in tamplet
context = {
'profiles': Profiles.objects.filter(Q(user_id__startswith=query) | Q(name__startswith=query))
}
return render(request, 'FB_search/home.html', context)
2)在我的端点中调用上面的函数,例如:
def api_search(request):
if request.method == "GET":
query = request.GET.get('q')
search = 'some API with {query} inside it'
search_query(request,search,query)
当我尝试调用“搜索查询”功能时,出现提示错误。 有什么建议吗?
谢谢。
答案 0 :(得分:1)
您没有返回search_query
的返回值,应该返回它:
def api_search(request):
if request.method == "GET":
query = request.GET.get('q')
search = 'some API with {query} inside it'
return search_query(request,search,query)