Reputation: 17
I have a search bar on my jobs_board.html page which works well. But I want to add this search bar to my home page so it performs the same search and redirects to the jobs_board.html page to display the results. Any suggestions on how I can add this the home.html and it's view.py
Thanks in advance.
jobs/views.py
def get_jobs_queryset(query=None):
queryset = []
queries = query.split(" ")
for q in queries:
posts = JobsPost.objects.filter(
Q(title__contains=q)|
Q(body__icontains=q)
).distinct()
for post in posts:
queryset.append(post)
def board_jobs_view(request):
context = {}
query = ""
if request.GET:
query = request.GET['q']
context['query'] = str(query)
jobs_posts = sorted(get_jobs_queryset(query), key=attrgetter('date_updated'), reverse=True)
context['jobs_posts'] = jobs_posts
return render(request, "jobs/jobs_board.html", context)
jobs_board.html
<div class="container">
<div class="center">
<h1 class="display-4">
<form method="get">
<input class="form-control form-control-lg" type="text" name="q" id="id_q" placeholder="Search Jobs">
</h1>
</div>
</div>
<script type="text/javascript">
document.getElementById("id_q").value = "{{query}}"
</script>
Upvotes: 1
Views: 519
Reputation: 477230
You specify the path of the search view as action="…"
attribute in the <form>
tag:
<form method="get" action="{% url 'board_job_view' %}">
<input class="form-control form-control-lg" type="text" name="q" id="id_q" placeholder="Search Jobs">
<button type="submit">
</form>
Here board_job_view
is the name of the view, so the path should look somethin like:
urlpatterns = [
# …,
path(… views.board_jobs_view, name='board_jobs_view')
# …,
]
Upvotes: 1