Reputation: 7957
I am trying to implement a document search engine in django. The main page has a text box in which the user is supposed to enter a query. On entering the query, the user will be redirected to a page that will contain the titles of document relevant to their query. However, I'm unable to redirect to a new page on query submission. My app is named search
and this is the page that loads on opening 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
file:
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"),
]
the views.py
file
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)
I want to redirect the results this page.
query_results.html
{{ context['query'] }}
Any ideas where I am going wrong?
Upvotes: 0
Views: 193
Reputation: 615
Probably url (r '^ $', views.index)
is preferentially executed.
Try changing index view. like this.
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)
Upvotes: 1
Reputation: 481
Your index view and your doc_scorer view seem to have the same url ('/' in both cases). Hence, if the form sends its data to '/', it will be processed by the index view, which comes first in the list of url patterns. You should change the url conf of your doc_scorer view:
urlpatterns = [...
url('^doc_scorer/$', views.doc_scorer, name='doc_scorer'),
]
Upvotes: 1