With Django I'm creating an encyclopedia website similar to Wikipedia, for a class. I've played around with this for quite a while but still cannot solve this - I keep getting the error: entry() missing 1 required positional argument: 'title'
The error occurred when I clicked to create a new page on the site. This pertains to the function 'newpage' but I think the issue might be in the function 'entry.'
I did the exact steps (but using 'title') from this Stack Overflow suggestion but it did not work: TypeError at /edit : edit() missing 1 required positional argument: 'entry'
In urls.py I've tried both:
path("wiki/<str:entry>", views.entry, name="entry"),
path("wiki/<str:title>", views.entry, name="entry"),
Just can't figure out how to get past the error. Any info is appreciated, as I'm new to Django.
urls.py file:
urlpatterns = [
path('admin/', admin.site.urls),
path("", views.index, name="index"),
path("wiki/<str:title>", views.entry, name="entry"),
path("newpage", views.entry, name="newpage"),
]
views.py file:
class CreateForm(forms.Form):
title = forms.CharField(label='title')
text = forms.CharField(label='text')
def index(request):
"""Homepage for website"""
return render(request, "encyclopedia/index.html", {
"entries": util.list_entries()
})
def entry(request, title):
""" Displays the requested entry page, if it exists """
entry_md = util.get_entry(title)
if entry_md != None:
# Title exists, convert md to HTML and return rendered template
entry_HTML = Markdown().convert(entry_md)
return render(request, "encyclopedia/entry.html", {
"title": title,
"entry": entry_HTML,
})
else:
# Page does not exist
return render(request, "encyclopedia/error.html", {
"title": title,
})
def newpage(request):
if request.method == "GET":
return render(request, "encyclopedia/newpage.html", {
"create_form": CreateForm(),
"title": title,
})
elif request.method == "POST":
form = CreateForm(request.POST)
if form.is_valid():
title = form.cleaned_data['title']
text = form.cleaned_data['text']
entries = util.list_entries()
if title in entries:
return render(request, "encyclopedia/newpageerror.html", {"message": "Page
already exists"})
else:
util.save_entry(title, text)
title = util.get_entry(title)
messages.success(request, f'New page "{title}" created successfully!')
return render(request, "encyclopedia/newpage.html")