'Page not found' error (Python Crash Course exercises)

In the 2nd Edition Python Crash Course, Chapter 19 - Letting Users Edit Entries:

I’ve made the coding changes to urls.py and created the edit_entry.html page.

To test the new edit functionality I pull up the page to make edits to the ‘Chess’ topics page:

localhost:8000/edit_entry/1/

The Chess topic has an id of ‘1’.

I make the edits and then click ‘Save changes.’ (Another issue I’m seeing is the ‘Entry:’ label which should not be on this page).

When I go to save the changes (Save changes) I get the Page not found error.

The server seems to be complaining about not finding the Id of the Chess topic, which in this case is ‘1’, I believe. The error message coming back seems to indicate that it’s looking for localhost:8000/edit_entry/10/ as opposed to ‘1’. is that possible? Could it be a ‘browser thing’? I’m using Safari on the MacBook Pro.

That setup for that URL would be in the urls.py file in the /learning_log/learning/logs/ directory. I’ve rechecked the code a dozen times and have even replaced the verision of urls.py with the solution code in the Chapter 19 folder of the exercises I downloaded. So, at this point, I’m using the ‘stock’ code for urls.py.

Below are my screen attachments and code for urls.py.

Any assistance would be appreciated.

-- Vin

urls.py:

"""Defines URL patterns for learning_logs."""

from django.urls import path

from . import views

app_name = 'learning_logs'
urlpatterns = [
    # Home page
    path('', views.index, name='index'),
    # Page that shows all topics.
    path('topics/', views.topics, name='topics'),
    # Detail page for a single topic.
    path('topics/<int:topic_id>/', views.topic, name='topic'),
    # Page for adding a new topic.
    path('new_topic/', views.new_topic, name='new_topic'),
    # Page for adding a new entry.
    path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),
    # Page for editing an entry.
    path('edit_entry/<int:entry_id>/', views.edit_entry, name='edit_entry'),
]

The edit_entry.html file:

{% extends "learning_logs/base.html" %}

{% block content %}

<p><a href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a></p>

<p>Edit Entry</p>

<form action="% url 'learning_logs:edit_entry' entry.id %}" method='post'>
	{% csrf_token %}
	{{ form.as_p }}
	<button name ="submit">Save changes</button>
</form>

{% endblock content %}

Welcome @vpcocco-eng !

Look at the details of the error message. Look at the url that you’re trying to request.

Then take a close look at this line and see if you can recognize what’s wrong.

Hi there! Thanks for responding.

I’ve looked at this for a bit and I thinks it’s important for a new user to try and figure this things out on there own. But at this point I’m a little stumped.

  • I’ve confirmed that edit_entry.html is in the same directory as the other html files. This location would be (as instructed by the text): …/learning_log/learning_logs/templates/learning_logs.

The URL is showing this:

When I click ‘Save changes’ the error message is referencing the ID ‘9’:

The current path, edit_entry/9/% url 'learning_logs:edit_entry' entry.id %}, didn’t match any of these.

Still not sure why the ‘Entry:’ label is appearing on this page.

Thanks again.

Look again at this specific line:

Ahh - a typo! I was missing the left curly brace - {. I made the code edit and the error message has disappeared and I can now edit entries.

The line now reads:

<form action="{% url 'learning_logs:edit_entry' entry.id %}" method='post'>

Thank you again, sir, for providing a second set of eyes.

Much Appreciated,

Vin

Now that you have identified (and corrected) the error, do you understand why you were getting the specific error message you were receiving?

Yes, I believe so. The missing ‘{’ was creating a malformed URL address which the server could not resolve. Is that correct?

Thanks again,

Vin

Essentially, yes. Missing the { meant that the template rendered % url 'learning_logs:edit_entry' entry.id %} as a literal string for the url for the action attribute instead of using the url tag to locate the proper url.

That’s why you’re seeing
%25%20url%20'learning_logs:edit_entry'%20entry.id%20%25%7D
in the requested url.

(The %25 is the % character, %20 is the space, and %7D is }.)