The fault is : TypeError at /topics/1/
Topic() got unexpected keyword arguments: ‘topic_id’ here is the steps
the s=runserver is activated (python manage.py runserver)
From my browser :http://127.0.0.1:8000 I get the good screen but when I go to http://127.0.0.1:8000/topics/1/ I receive the error Topic() got unexpected arguments:‘topic_id’
from the shell I can get these informations :
>>> topic = get_object_or_404(Topic, id=1)
>>> entries = topic.entry_set.order_by('-date_added')
>>> print(entries)
<QuerySet [<Entry: Topic 2>, <Entry: Topic 1>]>
>>> context = {'topic': topic, 'entries': entries}
>>> print(context)
{'topic': <Topic: Topic 1>, 'entries': <QuerySet [<Entry: Topic 2>, <Entry: Topic 1>]>}
>>>
>>> topic = get_object_or_404(Topic, id=topic_id)
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'topic_id' is not defined
>>>
This confirm that there is some thing wrong with the arguent ‘topic_id’
here is my urls.py
''' Define urls.py for the directory learning_logs '''
# from django.conf.urls import url #cor 26/12/2023
from django.urls import re_path as url
from . import views
urlpatterns = [
# home page
url('^$', views.index, name='index'),
# show all topics
url(r'^topics/$', views.topics, name='topics'),
# detail page for a single topic
url(r'^topics/(?P<topic_id>\d+)/$' , views.Topic , name='topic'),
]
here is my views.py
""" From learning_log/learning_logs """
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from learning_logs.models import Topic # Import the Topic model
# 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_list = Topic.objects.order_by('date_added') # Use a different variable name
context = {'topics': topics_list}
return render(request, 'learning_logs/topics.html', context)
#def topic(request, topic_id):
"""Show a single topic with its entries."""
topic = get_object_or_404(Topic, id=topic_id)
entries = topic.entry_set.order_by('-date_added')
context = {'topic': topic, 'entries': entries}
return render(request, 'learning_logs/topic.html', context)
I have just see a similar problem I will look at it , in the same time if some one find a solution it will be wanderfull.
Best regards
Alain