I’m working through the Python Crash Course book by Eric Matthes. In Chapter 19 i start to run into problems. The ‘Edit Entry’ section does not load correctly when clicking the link. The link displays where it should, with the text that it should, but when clicking the link i get this message in my browser:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/topics/1/%7Burl%20'learning_logs:edit_entry'%20entry.id%7D
Using the URLconf defined in ll_project.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
topics/ [name='topics']
topics/<int:topic_id>/ [name='topic']
new_topic/ [name='new_topic']
new_entry/<int:topic_id>/ [name='new_entry']
edit_entry/<int:entry_id>/ [name='edit_entry']
The current path, topics/1/{url 'learning_logs:edit_entry' entry.id}, didn’t match any of these.
runserver shows this output in the terminal:
Not Found: /topics/1/{url 'learning_logs:edit_entry' entry.id}
[10/Jul/2024 09:49:24] "GET /topics/1/%7Burl%20'learning_logs:edit_entry'%20entry.id%7D HTTP/1.1" 404 3357
Django Version 5.0.6
Python Version 3.12.4
Here is the relevant code i have so far in the project for reference:
**learning_logs/views.py:**
from django.shortcuts import render, redirect
from .models import Topic, Entry
from .forms import TopicForm, EntryForm
def index(request):
''' The home page for learning_logs '''
return render(request, 'learning_logs/index.html')
def topics(request):
''' Show all topics '''
topics= Topic.objects.order_by('date_added')
context= {'topics': topics}
return render(request,'learning_logs/topics.html',context)
def topic(request, topic_id):
''' Show a single topic and all it's entries '''
topic= Topic.objects.get(id=topic_id)
entries= topic.entry_set.order_by('-date_added')
context= {'topic': topic, 'entries': entries}
return render(request,'learning_logs/topic.html',context)
def new_topic(request):
''' Add a new topic '''
if request.method != 'POST':
# No data submitted: create a blank form
form=TopicForm()
else:
# POST data submitted; process data.
form=TopicForm(data=request.POST)
if form.is_valid():
form.save()
return redirect('learning_logs:topics')
# Display a blank or invalid form.
context={'form': form}
return render(request,'learning_logs/new_topic.html',context)
def new_entry(request, topic_id):
''' Add a new entry for a particular topic '''
topic=Topic.objects.get(id=topic_id)
if request.method != 'POST':
# No data submitted; create a blank form
form=EntryForm()
else:
# POST data submitted, process data
form=EntryForm(data=request.POST)
if form.is_valid():
new_entry=form.save(commit=False)
new_entry.topic=topic
new_entry.save()
return redirect('learning_logs:topic', topic_id=topic_id)
# Display a blank or invalid form
context = {'topic':topic,'form': form}
return render(request,'learning_logs/new_entry.html', context)
def edit_entry(request, entry_id):
''' edit an existing entry '''
entry=Entry.objects.get(id=entry_id)
topic=entry.topic
if request.method != 'POST':
# Initial request, pre-fill form with current entry
form = EntryForm(instance=entry)
else:
# POST data submitted; process data
form=EntryForm(instance=entry, data=request.POST)
if form.is_valid():
form.save()
return redirect('learning_logs:topic', topic_id=topic.id)
context={'entry':entry,'topic': topic,'form': form}
return render(request,'learning_logs/edit_entry.html',context)`
**learning_logs/urls.py:**
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 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'),`
**learning_logs/templates/edit_entry.html:**
{% extends "learning_logs/base.html" %}
{% block content %}
<p><a href="{% url 'learning_logs:topic' topic.id %}">{{ topic }}</a></p>
<p>Add a new entry:</p>
<form action="{% url 'learning_logs:new_entry' topic.id %}" method='post'>
{% csrf_token %}
{{ form.as_div }}
<button name='Submit'>Add entry</button>
</form>
{% endblock content %}`
**learning_logs/templates/topic.html:**
{% extends 'learning_logs/index.html' %}
{% block content %}
<p>Topic: {{topic.text}}</p>
<p>Entries:</p>
<p>
<a href="{% url 'learning_logs:new_entry' topic.id %}">Add new entry</a>
</p>
<ul>
{% for entry in entries %}</ul>
<li>
<p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
<p>{{ entry.text|linebreaks }}</p>
<p>
<a href="{% url 'learning_logs:edit_entry' entry.id %}">
Edit entry</a>
</p>
</li>
{% empty %}
<li>There are no entries for this topic yet.</li>
{% endfor %}
{% endblock content %}`
I’ve reviewed my code against the code in the book, as well as the related github page, to see if maybe there were syntax errors, typos, updated code on his github, etc. and everything looks correct. When i enter the URL in my browser it brings me to the correct page, but the link seems to be adding in all the text, including curly braces, that is referenced. The quotations seem to be properly formatted and matched up. A google search did not proved any seemingly relevant answers, more of what i found seemed to address the problem of getting braces into the output, rather than keeping them from ‘leaking’ into it. I can’t find any real difference between how other links/urls were added to the project and this one. everything else works as expected.
chaos_daemon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.