Home > Software engineering >  While trying to reverse a url in django template html file, exception 'NoReverseMatch' occ
While trying to reverse a url in django template html file, exception 'NoReverseMatch' occ

Time:06-11

def entry(request, name):
    content = util.get_entry(name.strip())
    if content == None:
        content = "## Page was not found"
    content = markdown(content)
    return render(request, "encyclopedia/entry.html", {'content': content, 'title': name})

def edit(request,title):
    content = util.get_entry(title.strip())
    if content == None:
        return render(request, "encyclopedia/edit.html", {'error': "404 Not Found"})
    
    if request.method == "POST":
        content = request.POST.get("content").strip()
        if content == "":
            return render(request, "encyclopedia/edit.html", {"message": "Can't save with empty field.", "title": title, "content": content})
        util.save_entry(title, content)
        return redirect("entry", name=title)
    return render(request, "encyclopedia/edit.html", {'content': content, 'title': title})

util has files that help get names of entered files, save a new entry or get content of entry.

{% extends 'encyclopedia/layout.html' %}

{% block title %}
   {{title}}
{% endblock %}

{% block body %}
<a href="{% url 'edit' title %}" >Edit This Page</a>
    {{entry | safe }}
{% endblock %}

layout has the standard block code of HTML

edit.html contains HTML code that gives a button on each page so that we can edit the content of the page entry and passes name='content' for the content to be edited.


urlpatterns = [
    path("", views.index, name="index"),
    path("wiki/<str:name>", views.entry, name = 'entry'),
    path("search", views.search, name= 'search'),
    path("newpage",views.create,name='create'),
    path("random",views.random,name='random'),
    path("edit", views.edit, name='edit')
]

THIS IS A CS50w project and I have taken references from other sources.

CodePudding user response:

You forgot to add title parameter to edit path

path("edit/<str:title>/", views.edit, name='edit')

  • Related