Django Project 1 implementation issue
I am trying to complete a project and it is not working as intended. This is the following requirements:
Search: Allow the user to type a query into the search box in the sidebar to search for an encyclopedia entry. If the query matches the name of an encyclopedia entry, the user should be redirected to that entry’s page. If the query does not match the name of an encyclopedia entry, the user should instead be taken to a search results page that displays a list of all encyclopedia entries that have the query as a substring. For example, if the search query were Py, then Python should appear in the search results. Clicking on any of the entry names on the search results page should take the user to that entry’s page.
This is my code:
util.py
import re from django.core.files.base import ContentFile from django.core.files.storage import default_storage def list_entries(): """ Returns a list of all names of encyclopedia entries. """ _, filenames = default_storage.listdir("entries") return list(sorted(re.sub(r"\.md$", "", filename) for filename in filenames if filename.endswith(".md"))) def save_entry(title, content): """ Saves an encyclopedia entry, given its title and Markdown content. If an existing entry with the same title already exists, it is replaced. """ filename = f"entries/{title}.md" if default_storage.exists(filename): default_storage.delete(filename) default_storage.save(filename, ContentFile(content)) def get_entry(title): """ Retrieves an encyclopedia entry by its title. If no such entry exists, the function returns None. """ try: f = default_storage.open(f"entries/{title}.md") return f.read().decode("utf-8") except FileNotFoundError: return None
views.py
from django.shortcuts import render from . import util def index(request): return render(request, "encyclopedia/index.html", { "entries": util.list_entries() }) def wiki(request, title): return render(request, "encyclopedia/wiki.html", { "entries": util.get_entry(title) }) def search(request): entries = util.list_entries() find_entries = list() search_box = request.POST.get("q", "").capitalize() if search_box in entries: return HttpResponseRedirect(f"wiki/{search_box}") for entry in entries: if search_box in entry: find_entries.append(entry) else: print(f'{find_entries}') if find_entries: return render(request, "encyclopedia/search.html", { "search_result": find_entries, "search": search_box })
What is happening when i put CSS in the search box is its going to the search.html page and not the entries page (it should be running the entry function) …Everything else works properly. Can you help me?