0

I am working on CS50 Project 1 dealing with Django. In urls.py I have two paths that take strings as input, but neither work, and I receive a NoReverseError message.

urls.py code

urlpatterns = [
    path("", views.index, name="index"),
    path("edit_entry/<str:title>/", views.edit_entry, name = "edit_entry"),
    path("search/", views.search, name = "search"),
    path("wiki/<str:title>/", views.get_entry, name = "get_entry"),
    path("create_entry", views.create_entry, name = "create_entry") 
]

views.get_entry code

def get_entry(request, title):
    exists = util.get_entry(title)
    if exists is None:
            return render(request, "encyclopedia/get_entry.html", {
                "entry": "Entry Does Not Exist"
            })
    else:
        entry = markdown(util.get_entry(title))
        return render(request, "encyclopedia/get_entry.html", {
            "entry": entry
             })

views.edit_entry code (edit_entry actually has some more work that needs to be done to it)

def edit_entry(request, title):
   if request.method == "POST":
       form = NewEditEntryForm(request.POST)
       if form.is_valid():
           title = form.cleaned_data["title"]
           content = form.cleaned_data["content"]
           util.save_entry(title, content)
           return HttpRespnseRedirect("/wiki/" + title)
   else: return render(request, "encyclopedia/edit_entry.html",{
       "form": NewEditEntryForm() })

The error message

NoReverseMatch at /wiki/Css/
Reverse for 'edit_entry' with keyword arguments '{'title': ''}' not found. 1 pattern(s) tried: ['edit_entry/(?P<title>[^/]+)/$']

Your help would greatly be appreciated. Thank you!

Templates get_entry.html

{% extends "encyclopedia/layout.html" %}

{% block title %}
    Encyclopedia
{% endblock %}

{% block body %}
   
    <p>
            {{ entry|safe }}
    </p>
    <a href = "{% url 'edit_entry' title=title %}">Edit This Entry</a>
{% endblock %}

edit_entry.html

{% extends "encyclopedia/layout.html" %}

{% block title %}
    Edit Entry
{% endblock %}

{% block body %}
    <h1>Edit {{ title }}</h1>
    <form action = "{% url 'edit_entry' title=title %}" method = "POST"></form>
        {% csrf_token %}
        {{ form }}
        <input type = submit value = "Save">
    </form>
{% endblock %}
skateb2020
  • 129
  • 11
  • where does the NoReverseError happen? If you try to access the url in browser, I think you would get http 404 status instead. can you show us where the reverse happens? – Kryštof Řeháček Jul 25 '21 at 19:42
  • That path requires a string to act as the `title` in the view. Can you include the code that creates the error? Maybe it's coming from a template? I can't see a `reverse` here. Is `HttpRespnseRedirect("/wiki/" + title)` causing this? You'll need to debug what the `title` value is there if it is that. – markwalker_ Jul 25 '21 at 19:50
  • Can you add your template? – Martins Jul 25 '21 at 23:13
  • The error is happening in the browser when I put in the url. For example it happens with http://127.0.0.1:8000/wiki/Css/ – skateb2020 Jul 26 '21 at 13:07
  • get_entry.html `{% extends "encyclopedia/layout.html" %} {% block title %} Encyclopedia {% endblock %} {% block body %}

    {{ entry|safe }}

    Edit This Entry {% endblock %}`
    – skateb2020 Jul 26 '21 at 13:11
  • edit_entry.html `{% extends "encyclopedia/layout.html" %} {% block title %} Edit Entry {% endblock %} {% block body %}

    Edit {{ title }}

    {% csrf_token %} {{ form }} {% endblock %}` I have posted the templated @Martins.
    – skateb2020 Jul 26 '21 at 13:15
  • @KryštofŘeháček, under the NoReverseError it says Reverse for 'edit_entry' with keyword arguments '{'title': ''}' not found. 1 pattern(s) tried: ['edit_entry/$'] It also says Error during template rendering and has a portion of my layout.html with an error on line 8, but I didn't even write that code (it is a link to a stylesheet to bootstrap that was already written when I unzipped the code). Let me know if you need that template too. – skateb2020 Jul 26 '21 at 14:29
  • Add the templates to the question not the body, though not clear, I think you are not passing the argument for url "title" to template. – Martins Jul 27 '21 at 12:47
  • @Martins I added the templates to the question, so they are easier to read. Can you explain in more detail what I am doing wrong? – skateb2020 Jul 27 '21 at 13:27
  • Where is the variable title coming from – Martins Jul 27 '21 at 20:19
  • @Martins, the title variable in edit_entry is supposed to get the title input from the form. – skateb2020 Jul 27 '21 at 21:01
  • How are you getting it from the form. – Martins Jul 28 '21 at 13:10
  • @Martins, using form.cleaned_data – skateb2020 Jul 28 '21 at 13:43
  • You need to pass it in get request. Not post. – Martins Jul 28 '21 at 14:27
  • @Martins, I changed it to a get request and am still getting the error. – skateb2020 Jul 29 '21 at 14:41

1 Answers1

0

I figured it out. I had to remove title=title from the hrefs in the templates and make it a hard coded url with a variable.

skateb2020
  • 129
  • 11