1

For the Wiki project I'm having trouble implementing the search bar. I have spent about 2 days researching search bars in Django, but so far everything I've tried yields the same result:

No matter what I try, when I search for an article name that exists, the homepage just stays exactly the same, but only the URL changes to http://127.0.0.1:8000/?q=Apple. So for example, everything after the ?q= is the title of the article (this one is called Apple). But the page I'm on stays the same and doesn't go to the actual page I'm looking for. I'm wondering if the url is part of the problem because I know other search bars have the end of the url looking like: Search?q=Apple

urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:title>", views.entry, name="entry"),
path("search", views.search, name="search"),
]

If the query matches the title of an article entry, the user should be redirected to that entry’s page. Here is one of the examples I tried and all code associated in views.py:

views.py

get_entry.py

1
  • I have been working on a same project. q="searchterm" is a input that you will get by using title = request.POST["q"]. However, In form using "search/" a new path and take all entry list from list_entries and make a list of it and then compare each letter and use for loop to loop from all entries and compare title with all entries one by one by using if then save matched ones in list and render that list on search.html page.
    – backman101
    Commented Sep 4, 2023 at 4:55

3 Answers 3

1

Nevermind, I have been able to figure it out. I had to use a GET request.

0

http://127.0.0.1:8000/?q=Apple. is not the URL you should redirect the query. Use http://127.0.0.1:8000/search?q=Apple instead.

So, in your form you should have something like this:

<form action="{% url 'search' %}" method="get">

Also, set the search path before the entry path, in your urls.py:

path("search", views.search, name="search"), path("wiki/<str:title>", views.entry, name="entry"),

Hope this help

0

here is the code:

code: `def search(request):
if request.method == "POST":
    entry = request.POST['q'] #getting the value of q from the form as it's name is q
    html_content = convert_md_to_html(entry)
    if html_content is not None:
        return render(request, "encyclopedia/css.html", {
                "content": html_content
            })
    else:
        all_entries = util.list_entries()
        recommendation = []
        for entries in all_entries:
            if entry.lower() in entries.lower():
                recommendation.append(entries)
        return render(request, "encyclopedia/search.html", {
                "recommendation": recommendation
            })`

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .