0

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")
user14567126
  • 313
  • 3
  • 12
  • What is the URL you are going to? Does it look something like "localhost:8000/entry/sometitle"? – Dasith Dec 08 '20 at 02:02
  • As soon as I click the server link its http://127.0.0.1:8000/. Then when I click "Create new page" the url is http://127.0.0.1:8000/newpage. Maybe this is part of the problem? For the assignment the url has to be /wiki/TITLE, where TITLE is the title of the 'create a new page' page. I tried to write it this way in urls.py but now I'm not quite sure how to fix. – user14567126 Dec 08 '20 at 02:12
  • please show your templates – ha-neul Dec 08 '20 at 03:11

1 Answers1

0

The issue is you are saying that entry needs 2 parameters but only request is provided. You can change entry function to this:

def entry(request, title=None):
 # Rest of your code

Here I am setting title=None as the default parameter, this will fix the issue you have now, but could cause other issues. I'm not sure exactly why there are 2 urls that go to entry.

Dasith
  • 363
  • 2
  • 13