I spent my entire day trying to fix this bug and I have no idea how to do it. I am trying to create a new markdown file and redirect the user to the new page after submitting the form. I tried to change the URL name, tried to change variable names but nothing works. The problem may be the fact that Django adds a dollar sign in my URL pattern but I am not sure about it because I have tried to give the URL the value of an existing page. I would appreciate a detailed explanation for this problem. my views.py:
def entry(request, entry):
entryPage = util.get_entry(entry)
if entryPage is None:
message = messages.warning(request, 'There is not a page with this name.')
return render(request, 'encyclopedia/error.html', {
'entryTitle': entry,
'message': message
})
else:
return render(request, 'encyclopedia/entry.html',{
'entry': markdowner.convert(entryPage),
'title': entry
})
def new(request):
if request.method == 'POST':
# Get the form
form = Post(request.POST)
if form.is_valid():
# Clean the form
title = form.cleaned_data['title']
lower_title = form.cleaned_data['title'].lower()
text = form.cleaned_data['textarea']
entries = util.list_entries()
# Check for existing entry
for entry in entries:
if lower_title == entry.lower():
message = messages.warning(request, "There is already a page with this name.")
context = {
'message': message,
"form": Search(),
"post": Post()
}
return render(request, 'encyclopedia/new.html',context)
# Create and redirect to new entry
else:
util.save_entry(title,text)
return HttpResponseRedirect(reverse("entry", args=[title]))
else:
return render(request, 'encyclopedia/new.html', {
"form": Search(),
"post": Post()
})
The error I receive:
NoReverseMatch at /new
Reverse for 'entry' with arguments '('NewTitle',)' not found. 1 pattern(s) tried: ['entry$']
My urls.py:
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
path("wiki/<str:entry>", views.entry, name="title"),
path("new", views.new, name="new"),
path("error", views.error, name="error"),
path("entry", views.entry, name="entry")
]
Also when I check the local vars inside the Django Traceback, I don't see the title from. This may be caused because the iteration is executed just once until the error occurs.
CodePudding user response:
You forgot to add a parameter for the entry
to your entry URL pattern:
urlpatterns = [
# …,
path('entry/<str:entry>/', views.entry, name='entry')
]