Reputation: 21
This is the extention of the same problem i asked about in a previous question.
The problem i'm having here now is that after fixing the problem with the trans views function and expecting the program to run without problems, i get the following error
NoReverseMatch at /wiki/Python/edit
Reverse for 'edit' with keyword arguments '{'title': ''}' not found.
Which i can't for the life of me understand considering that i AM passing the title argument in the html and set up the edit views function and the urlpath in urls.py to expect the title argument.
when i take a closer look at the traceback, it highlights this block of code from the edit views function.
return render(request, "encyclopedia/edit.html", {
"form": EntryForm(initial={
"title": title,
"content": entry
})
})
but i don't understand what the problem is here?
this is the final part of the project i'm working on so i'll be very grateful for any and all help.
below is the relevant code.
HTML
entry.html
<!-- The edit button in the entry's page -->
<form action="{% url 'wiki:trans' %}" method="POST">
{% csrf_token %}
<input type=hidden value={{title}} name="title">
<input type=submit value="Edit">
</form>
edit.html
<!-- the edit page -->
{% block body %}
<h1>Edit Entry</h1>
<br>
<form action="{% url 'wiki:edit' title=title %}" method="POST">
{% csrf_token %}
{{form}}
<input type="submit" value="Save" id="save">
</form>
{% endblock %}
view.py
class EntryForm(forms.Form):
title = forms.CharField(label="Title")
content = forms.CharField(widget=forms.Textarea)
def trans(request):
title = request.POST.get("title")
return redirect("wiki:edit", title=title)
def edit(request, title):
if request.method == "GET":
entry = util.get_entry(title)
return render(request, "encyclopedia/edit.html", {
"form": EntryForm(initial={
"title": title,
"content": entry
})
})
else:
form = EntryForm(request.POST)
if form.is_valid():
title = form.cleaned_data["title"]
content = form.cleaned_data["content"]
util.save_entry(title, content)
return redirect("wiki:title", title=title)
urls.py
app_name = "wiki"
urlpatterns = [
path("", views.index, name="index"),
path("search", views.search, name="search"),
path("new", views.new, name="new"),
path("trans", views.trans, name="trans"),
path("<str:title>/edit", views.edit, name="edit"),
path("random", views.rand, name="random"),
path("<str:title>", views.title, name="title")
]
Upvotes: 0
Views: 241
Reputation: 1744
The title
var that you are passing to title=title
dict is empty that means that the title variable has no data. To fix it make sure that title has a value or change that as optional argument in the url patterns.
You can fix it doing the following:
def edit(request, title=None):
Then title will take a default value if you don't pass it.
Upvotes: 0
Reputation: 33335
def edit(request, title):
if request.method == "GET":
entry = util.get_entry(title)
return render(request, "encyclopedia/edit.html", {
"form": EntryForm(initial={
"title": title,
"content": entry
})
})
That render()
call is not passing a value for title
, therefore the template tries to use a blank value, which is not allowed.
Upvotes: 1