TemplateDoesNotExist at /new_topic/

I’m working out of a text book Python Crash Course third edition in chapters 18 and 19. I writing the code word for word line by line. I believe my issue is in views.py. When I run python manage.py runserver the browser opens to localhost:8000. But when I try to click on new topic link I get the error above. Here is my views.py code

from django.http import HttpResponseRedirect

#from importlib.metadata import EntryPoints, entry_points
from django.shortcuts import render, redirect
#from pkg_resources import EntryPoint

Create your views here.

from .models import Topic, Entry
from .forms import TopicForm, EntryForm

def index(request):
“”“The home page for learning Log.”“”
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 its 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)

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(instanc=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)
               
# Display a blank or invalid form.
context = {'entry': entry, 'topic': topic, 'form': form}
return render(request, 'learning_logs/edit_entry.html', context)

Here is my learning_logs/urls code

Blo"““Define 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'),

]ckquote

A couple things -

  • When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. Do this for each file that you’re posting here.

  • There’s more information that gets printed than just the name of the error. Whenever you’re asking for help here, always include the complete error, including the full traceback. (Also, surround this information between lines of ``` just like the code.)

  • Show your directory structure, including the location of your template files.

  • Post your TEMPLATES setting from your settings.py file.

‘’’ I have a similar problem ‘’’
‘’’ my deposal directory for my html files is C:\learning_log\learning_logs\templates\learning_logs’‘’
‘’’ Directory of C:\learning_log\learning_logs\templates\learning_logs

31/12/2023 22:41 .
31/12/2023 22:41 …
31/12/2023 23:29 147 base.html
31/12/2023 22:51 182 index.html
01/01/2024 22:58 265 topics.html ‘’’

‘’’ My topics.html

{% extends “learning_logs/base.html” %}

{% block content %}

Topics

    {% for topic in topics %}
  • {{ topic }}
  • {% empty %}
  • No topics have been added yet.
  • {% endfor %}
{% endblock content %}

‘’’

‘’’ My views.py ‘’’

“”" From learning_log/learning_logs “”"
from django.shortcuts import render
from .models import Topic
‘’‘# Create your views here.’‘’

def index(request):
“”" the home page from learning_log “”"
return render(request, ‘learning_logs/index.html’)

‘’’ #def topics(request):
‘’‘# show all topics’‘’
‘’‘# topics = Topic.objects.order_by(‘date_added’) #date_added’‘’
‘’‘# context = {‘topics’:topics}’‘’
‘’‘# return render(request,‘learning_logs/topics.html’, context)’‘’

‘’‘# ADD test 01/01/2024’‘’
def topics(request):
“”“show all topics”“”
topics = Topic.objects.order_by(‘date_added’)
print(topics) # Add this line for debugging
context = {‘topics’: topics}
return render(request, ‘learning_logs/topics.html’, context)

‘’’ The runserver find the data requested (200) ‘’’

‘’’ C:\learning_log>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks…

System check identified no issues (0 silenced).
January 02, 2024 - 21:52:08
Django version 3.2.18, using settings ‘learning_log.settings’
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
<QuerySet [<Topic: Topic 1>, <Topic: Topic 2>]>
[02/Jan/2024 21:52:23] “GET /topics/ HTTP/1.1” 200 86
,
‘’’ but the display on my browser is

learning_logs - topics" }‘’’

Chrome

‘’’ My setting.py contain ‘’’

TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: ,
‘APP_DIRS’: True,
‘OPTIONS’: {
‘context_processors’: [
‘django.template.context_processors.debug’,
‘django.template.context_processors.request’,
‘django.contrib.auth.context_processors.auth’,
‘django.contrib.messages.context_processors.messages’,
],
},
},
]

‘’’ seams that the data base is working fine

C:\learning_log>python manage.py shell
Python 3.11.1 (tags/v3.11.1:a7a450f, Dec 6 2022, 19:58:39) [MSC v.1934 64 bit (AMD64)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
(InteractiveConsole)

from learning_logs.models import Topic
first_topic = Topic.objects.first()
print(first_topic)
Topic 1
specific_topic = Topic.objects.get(id=1)
print(specific_topic)
Topic 1 ‘’’

‘’’ Seams that django does not find his views.py to display the informations to the browser screen ‘’’

First, I’d suggest you open a new topic for this discussion since this may well be a different issue.

Also, note that you need to use the backtick - ` to mark off your code blocks, with the three backtick characters on a line by themselves before and after the block of code. It’s a line of ```, then the code, then another line of ```. The ``` must not be part of the same line as any other text. Make sure you use the backtick and not the apostrophe - ' or any other “smart quote” from a word processor/text editor.

Ok ken , I will do it . sorry for the wrong format.

Best regards and beste wishes for 2024