Anda dapat melakukan pencarian sederhana di sini. Anda tidak memerlukan panggilan POST atau pembuatan formulir. Namun, jika Anda ingin membuat formulir, ini harus tetap mengarahkan Anda ke arah yang benar.
Coba sesuatu seperti ini:
search.html:
<form method="get" action="/search/">
Search Notecards:<input type="text" name="q" id="id_q" value="{{ query }}"/>
<input type="submit" value="Search" />
</form>
views.py:
from myapp.models import Book
from django.template import RequestContext
from django.shortcuts import render_to_response
def search(request):
query = request.GET.get('q')
try:
query = int(query)
except ValueError:
query = None
results = None
if query:
results = Book.objects.get(uid=query)
context = RequestContext(request)
return render_to_response('results.html', {"results": results,}, context_instance=context)
result.html:
{% if results %}
{% for result in results %}
{{ result.uid }}
{{ result.xxxx }}
{{ result.xxxx }}
{% endfor %}
{% else %}
<h3 class='error'>Please enter a valid UID</h3>
<form method="get" action="/search/">
Search Notecards:<input type="text" name="q" id="id_q" value="{{ query }}"/>
<input type="submit" value="Search" />
</form>
{% endif %}