hello i am trying to add an edit page to my django url patterns and the page give me an error once i click on one of the links on my page the system crashes and i am presented with a no reverse match error from the system. If i do swap the the edit path with the entry path it does work then but that is not the way i want it to work. I would like the user be presented with entry page first and then be able to click on the edit button to edit the page. below are my views and some html pages.
new to django and looked into the documentation but didn't find anything. thank you for the help
below is also the error:
error - Reverse for 'edit' with arguments '('# Django\r\n\r\nDjango is a web framework written using Python that allows for the design of web applications that generate HTML dynamically.\r\n',)' not found. 1 pattern(s) tried: ['(?P[^/]+)$']
urls.py:
from django.urls import path
from . import views
app_name = "enc"
urlpatterns = [
path("", views.index, name="index"),
path("new_page", views.new_page,name="check_page"),
path("",views.get_search,name="search"),
path("<str:title>",views.entry, name="page"),
path("<str:title>",views.edit, name ="edit"),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse
from . import util
entries = {}
def index(request):
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def entry(request, title):
if util.get_entry(title):
return render(request,"encyclopedia/entry.html", {
"title": util.get_entry(title),
"name":title
})
else:
return render(request, "encyclopedia/error.html")
def get_search(request):
if request.method == 'GET':
form = util.search_form(request.GET['q'])
if form.is_valid():
search = form.cleaned_data["search"]
return render(request, "encyclopedia/entry.html",{
"search":util.get_entry(search)
})
def new_page (request):
if request.method == 'POST':
form = util.new_entry(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
details = form.cleaned_data['details']
if title in entries:
return render (request, "encyclopedia/error.html")
else:
util.save_entry(title, details)
entries[title] = details
return HttpResponseRedirect(f'{title}')
return render(request, "encyclopedia/new_page.html",{
"form": util.new_entry()
})
def edit (request,title):
entry = util.get_entry(title)
if entry == None:
return render(request, "encyclopedia/error.html")
else:
form = util.edit(initial={'title':title, 'details':entry})
return render (request, "encyclopedia/edit.html", {
"form": form,
'title': title,
'details':util.get_entry(title),
} )
html for edit:
{% extends "encyclopedia/layout.html" %}
{% block title %}
Encyclopedia
{% endblock %}
{% block body %}
<form action="{% url 'enc:edit' title %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
{% endblock %}
html for entry
{% block title %}
{{name}}
{% endblock %}
{% block body %}
<p>{{title}}</p>
<a href="{% url 'enc:index' %}">Go back to main page</a>
<a href="{% url 'enc:edit' title %}">Click here to edit the page</a>
{% endblock %}