In a Django application I am building as a project for an online course, I am expecting a link in one template (entry.html) to direct to a path ("edit") in urls.py with a variable in the url. This should initiate a function called edit in views.py and render the template edit.html.
I am getting a NoReverseMatch error ( "Reverse for 'edit' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P<entry>[^/]+)/edit$']"
) after clicking the link in entry.html. If I view page source while on entry.html in the development server, I can see the url matches that in urls.py but I still get this error.
In the example below, "maggie" is the value for entryTitle
I'm trying to pass.
entry.html:
{% block title %}
{{ entryTitle }}
{% endblock %}
{% block body %}
{{ entry|safe }}
<button>
<a href="{% url 'edit' entry=entryTitle %}">Edit Entry</a>
</button>
{% endblock %}
urls.py (last path listed is edit
)
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:entry>", views.entry, name="entry"),
path("search", views.search, name="search"),
path("new", views.new_page, name="new"),
path("wiki/<str:entry>/edit", views.edit, name="edit")
]
edit function in views.py also displaying my entry
function
from django.http import HttpResponseRedirect
from django.urls import reverse
class EditPageForm(forms.Form):
content = forms.CharField(
widget=forms.Textarea(),
label="Edit Content:")
def edit(request, entry):
if request.method == "POST":
#Edit file and redirect
form = EditPageForm(request.POST)
if form.is_valid():
content = form.cleaned_data["content"]
util.save_entry(entry, content)
return HttpResponseRedirect(reverse('entry', kwargs={'entry': entry}))
else:
#Load form with initial values filled
content = util.get_entry(entry)
form = EditPageForm(initial={"content": content})
return render(request, "encyclopedia/edit.html", {
"editform": form,
"entryTitle": entry
})
def entry(request, entry):
markdowner = Markdown()
entryPage = util.get_entry(entry)
if entryPage is None:
return render(request, "encyclopedia/notfound.html", {
"entryTitle": entry
})
else:
return render(request, "encyclopedia/entry.html", {
"entry": markdowner.convert(entryPage),
"entryTitle": entry
})
Is anyone able to see what is causing this error with my code? I am surprised because when viewing page source, it seems that {% url 'edit entry=entryTitle %}
is being correctly interpreted as wiki/maggie/edit
, which is present in urls.py with maggie
as <str:entry>
and yet I am getting this error.
The error message is my fault. I thought a single parameter could fill two variables in the path string but I guess not. You have two options.
path("wiki/edit/<str:entry>", views.edit, name="edit")
<button>
<a href="{% url 'edit' entry='maggie'%}">Edit</a>
</button>
This should work just like your entry
method.
path("wiki/<str:pathentry>/edit/<str:entry>")
<button>
<a href="{% url 'edit' pathentry='maggie' entry='maggie'%}">Edit</a>
</button>
I recommend the first but you know more about your project than I do.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.