0

I am simply trying to open the web app to the home page, but at startup I am given the NoReverseMatch error message. The full message is the following: Reverse for 'wiki' with no arguments not found. 1 pattern(s) tried: ['wiki/(?P<title>[^/]+)/$']. In case it's relevant, I am using Django version 3.2.4.

In urls.py for my project I have:

    urlpatterns = [
    path('', include("encyclopedia.urls", namespace="encyclopedia")),
    path('admin/', admin.site.urls),
]

In urls.py for my app I have:

app_name = "encyclopedia"

urlpatterns = [
    path("", views.index, name="index"),
    path("wiki/<title>/", views.entry, name="wiki"),
    .... (rest omitted for brevity)
]

In views.py I have:

def index(request):
    context = {
        "entries": ["item1", "item2", "item3"],
        "form": SearchForm(),
    }
    return render(request, "encyclopedia/index.html", context)

In index.html, I have:

{% block body %}

    <h1>All Pages</h1>

    <ul>
        {% for entry in entries %}
            <li>
                <form action="{% url 'encyclopedia:wiki' %}", method="POST">
                    {% csrf_token %}
                    <input type="text" value="{{ entry }}" name="link" hidden>
                    <input type="submit" class="link-btn" value="{{ entry }}" style="border: 0; background-color: white">
                </form>
            </li>
        {% endfor %}
    </ul>

{% endblock %}

As the error message indicates, it appears to be looking at the url pattern named wiki, and then subsequently failing to find the arguments passed to it. However, I am not trying to check that pattern; it should be going to the default pattern of path("", views.index, name="index"), not the wiki one. The wiki one works fine, by the way, as when I go the url wiki/<title>/, it loads fine. The rest of the app works fine as well. The other odd thing is that the index path worked fine before I added the arguments to the wiki url (path("wiki/<title>/", views.entry, name="wiki"),).

I have been looking at other SO posts that deal with this issue, and have followed the debugging advice given in this post, as well as this article, and none of the solutions apply/seem to work; this happens on app startup so there is no link being clicked to begin with, there are no comments it could be reading instead, it's not supposed to be calling wiki in the first place so the key word arguments are irrelevant, and the namespace is set in the project's urls.py. In summary, the issue appears to be that, upon start up of the app, it should be looking at the name "index" in the first path(), and then calling the appropriate function, but it is instead looking at the name "wiki" and expecting arguments.

If anyone can figure out how I can fix this problem, I would be very grateful!

SpencePro
  • 31
  • 1
  • 5
  • 1
    The form `
    ` tries to resolve the `wiki` route without arguments. Should it point at index instead?
    – Alexandr Tatarinov Jun 25 '21 at 20:23
  • @AlexandrTatarinov I don't think that is it, as that is the form on the index page that links to the other pages on the site. It should not be evaluating at all, as it is not being called; it should only be called when the user clicks the link, in which case it redirects to the `wiki` route and gets the relevant page. Changing the form url to `encyclopedia:index` indeed brings me to index.html on startup, but then all the links on index.html also bring me back to index.html, which breaks the site. – SpencePro Jun 25 '21 at 20:47
  • I think alexandr has the right idea - django attempts to translate the 'encyclopedia:wiki' to a url as it renders the template/reduces the template to HTML. Try removing that and seeing if your index renders, then if that's the problem you can debug why it isn't resolving 'encyclopedia:wiki' to the url - probably because you're expected to provide an argument there. – A. Rose Jun 25 '21 at 21:20
  • @AlexandrTatarinov @A.Rose Thank you, you are correct, changing the route to `index` worked, I just needed to then re-shuffle around some of the code in the other functions, and have them pass arguments to the `wiki` route. – SpencePro Jun 25 '21 at 22:27

0 Answers0