Home > database >  NoReverseMatch at /search/ Reverse for 'entry' not found. 'entry' is not a valid
NoReverseMatch at /search/ Reverse for 'entry' not found. 'entry' is not a valid

Time:01-11

NoReverseMatch at /search/ Reverse for 'entry' not found. 'entry' is not a valid view function or pattern name.

I´m trying to make an search engine for my website in django, via views.py, but Django always says that there's an exception in views.py

Views.py

def index(request):
return render(request, "encyclopedia/index.html", {
    "entries": util.list_entries()
})

def entries(request, entry):
  if entry not in util.list_entries():
        raise Http404
  content = util.get_entry(entry)
  return render(request,"encyclopedia/entry.html",
  {"title": entry, "content": Markdown().convert(content)},
   )

def search(request):
  query = request.GET.get("q", "")
  if query is None or query == "":
        return render(
              request,
              "encyclopedia/search.html",
              {"found_entries": "", "query": query},
        )

  entries = util.list_entries()

  found_entries = [
        valid_entry
        for valid_entry in entries
        if query.lower() in valid_entry.lower()
  ]
  if len(found_entries) == 1:
              return redirect("entry", found_entries[0])

  return render(
              request,
              "encyclopedia/search.html",
              {"found_entries": found_entries, "query": query},
  )

But Django says: "if len(found_entries) == 1: return redirect("entry", found_entries[0]) have an "NoReverseMatch" error"

Urls.py

from django.urls import path
from . import views, util

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

handler404 = 'encyclopedia.views.error_404_view'

Layout.html

{% load static %}
<!DOCTYPE html>
<html lang="en">
    <body>
        <div >
            <div >
                <h2>Wiki</h2>
                <form action="{% url 'search' %}">
                    <input  type="text" name="q" placeholder="Search 
Encyclopedia">
                </form>
                <div>
                    <a href="{% url 'index' %}">Home</a>
                </div>
                <div>
                    Create New Page
                </div>
                <div>
                    Random Page
                </div>
                {% block nav %}
                {% endblock %}
            </div>
            <div >
                {% block body %}
                {% endblock %}
            </div>
        </div>
    </body>
</html>

I've been trying a lot of things, but nothing fix it, how I can let my page run well?

CodePudding user response:

You must change your url name in urls.py to entry:

path("entries/<str:entry>", views.entries, name="entry"),

And you should pass the argument in your views.py file as:

return redirect("entry", entry=found_entries[0])

See https://docs.djangoproject.com/en/4.0/topics/http/shortcuts/#redirect

  •  Tags:  
  • Related