A class got unexpected keyword argument

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

Your urls.py file is incorrect.

The topic detail url is mapped to the Topic model class instead of the topic function view (wich is not defined as the def topic line is commented, which is probably wrong too).

antoine : if I change the code to

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'),      
]

and I correct the def topic() in the 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)


with the topics.html

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

{% block content %}

<p>Entries:</p>
<ul>
{% for entry in entries %}
  <li>
    <p>{{ entry.date_added|date:'M d, Y H:i' }}</p>
    <p>{{ entry.text|linebreaks }}</p>
  <li>
{% empty %}
  <li>
    There are no entries for this topic yet.
  <li>
{% endfor %}
<ul>   


<p>topics: {{ topic }}</p>
<ul>
{% for topic in topics %}
  <li>
     <a href="{% url 'learning_logs:topic' topic_id=topic.id %}">{{ topic }}</a>
  </li>
{% empty %} 
  <li>
    There are no entries for this topic yet.
  <li>
{% endfor %}
<ul>   



{% endblock content %}

My index.html

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

{% block content %}
<p>learning_logs helps you keep track of your learning, for topic you 're learning about. </p>
{% endblock content %}

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

{% block content %}

learning_logs helps you keep track of your learning, for topic you 're learning about.

{% endblock content %}

I still have a the error

emplateDoesNotExist at /topics/1/

learning_logs/topic.html

Request Method: GET
Request URL: http://127.0.0.1:8000/topics/1/
Django Version: 4.2.7
Exception Type: TemplateDoesNotExist
Exception Value: learning_logs/topic.html
Exception Location: C:\learning_log\ll_env\Lib\site-packages\django\template\loader.py, line 19, in get_template
Raised during: learning_logs.views.topic
Python Executable: C:\learning_log\ll_env\Scripts\python.exe
Python Version: 3.11.1
I have probably to compleet the html file, I will look at it

thanks for your support

Problem resolved , the urls.py does not call only 1 html but 2 html

''' 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'),      
]```

one is topic.html and the second is topics.html