我正在尝试在django中实现文档搜索引擎。主页面有一个文本框,用户应在其中输入查询。在输入查询时,用户将被重定向到包含与其查询相关的文档标题的页面。但是,我无法重定向到查询提交的新页面。我的应用名为search
,这是打开http://127.0.0.1:8000/search/
index.html
{% load static %}
<html>
<head>
<link rel="stylesheet" type="text/css" href="{% static 'search/style.css' %}" />
<title>muGoogle</title>
</head>
<body>
<div class = "container">
<h4 class = 'mugoogle'>muGoogle</h4>
<form class="form-wrapper" method = "GET" action = "{% url 'doc_scorer'%}">
<input type="text" name = 'query' class="textbox rounded" placeholder="Search docs related to..." required>
<input type="submit" class="button blue" value="Submit">
</form>
</div>
</body>
</html>
urls.py
档案:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index),
url(r'^search$', views.search),
url(r'^index$', views.index),
url(r'', views.doc_scorer, name="doc_scorer"),
]
views.py
文件
from django.template import Context, loader
from django.shortcuts import render, render_to_response
from django.http import Http404, HttpResponse, HttpResponseRedirect
import os
from other_code import some_function
def index(request):
template = loader.get_template("search/index.html")
return HttpResponse(template.render())
def search(request):
return HttpResponse("search is under construction")
def doc_scorer(request):
query = request.GET.get('query')
context = {'query':some_function(query)}
return render_to_response('query_results.html', context)
我想重定向此页面的结果。
query_results.html
{{ context['query'] }}
我出错的任何想法?
答案 0 :(得分:1)
您的索引视图和doc_scorer视图似乎具有相同的URL(在两种情况下都为“/”)。因此,如果表单将其数据发送到'/',它将由索引视图处理,该视图首先出现在url模式列表中。您应该更改doc_scorer视图的URL配置:
urlpatterns = [...
url('^doc_scorer/$', views.doc_scorer, name='doc_scorer'),
]
答案 1 :(得分:1)
可能优先执行url (r '^ $', views.index)
def index(request):
if not request.GET.get('query'):
return render_to_response('search/index.html', {})
# search execution
query = request.GET.get('query')
context = {'query':some_function(query)}
return render_to_response('query_results.html', context)
。
尝试更改索引视图。像这样。
.custom-counter {
margin: 0;
padding: 0;
list-style-type: none;
}
.custom-counter li {
counter-increment: step-counter;
margin-bottom: 10px;
}
.custom-counter li::before {
content: counter(step-counter, upper-roman);
text-decoration: underline;
padding: 3px 8px;
}