djangoでドキュメント検索エンジンを実装しようとしています。メインページには、ユーザーがクエリを入力するテキストボックスがあります。クエリを入力すると、ユーザはクエリに関連するドキュメントのタイトルを含むページにリダイレクトされます。ただし、クエリの送信時に新しいページにリダイレクトできません。私のアプリはsearch
名付け、これは開口部http://127.0.0.1:8000/search/
にロードするページdjangoのフォーム提出時にページをリダイレクトできない
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'] }}
私が間違っているつもり任意のアイデア?
エラーが発生しますか? –
@nik_mエラーはありません。リダイレクトはありません。投稿時にはURLだけが何も変わりません –
'doc_scorer'は実際に呼び出されますか? 'query = request.GET .....'の下に 'print(query)'を置き、実際に印刷されているかどうか確認してください。 –