# A class got unexpected keyword argument

**URL:** <https://forum.djangoproject.com/t/a-class-got-unexpected-keyword-argument/26916>\
**Category:** Mystery Errors\
**Created:** [January 13, 2024, 6:40am UTC](https://forum.djangoproject.com/t/a-class-got-unexpected-keyword-argument/26916 "2024-01-13T06:40:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ealalin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ealalin/32/17520_2.png) [@ealalin](https://forum.djangoproject.com/u/ealalin)\
**Post date:** [January 13, 2024, 6:40am UTC](https://forum.djangoproject.com/t/a-class-got-unexpected-keyword-argument/26916/1 "2024-01-13T06:40:40Z")

</div>

The fault is : TypeError at /topics/1/

Topic() got unexpected keyword arguments: ‘topic\_id’ here is the steps

 ![Django info.PNG](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/3X/3/6/36dcc5e1bb1c9450da3d8ab2e8522bcf06f4dd6a.jpeg)

the s=runserver is activated (python manage.py runserver)  
From my browser :[http://127.0.0.1:8000](http://127.0.0.1:8000) I get the good screen but when I go to [http://127.0.0.1:8000/topics/1/](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 :

```auto

>>> 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

```auto
''' 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

```auto
""" 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

---

<div class="post-metadata">

**Author:** ![antoinehumbert](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/antoinehumbert/32/14851_2.png) [@antoinehumbert](https://forum.djangoproject.com/u/antoinehumbert)\
**Post date:** [January 13, 2024, 10:56am UTC](https://forum.djangoproject.com/t/a-class-got-unexpected-keyword-argument/26916/2 "2024-01-13T10:56:04Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![ealalin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ealalin/32/17520_2.png) [@ealalin](https://forum.djangoproject.com/u/ealalin)\
**Post date:** [January 13, 2024, 4:22pm UTC](https://forum.djangoproject.com/t/a-class-got-unexpected-keyword-argument/26916/3 "2024-01-13T16:22:59Z")

</div>

antoine : if I change the code to

```auto
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

```auto
""" 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

```auto
{% 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

```auto
{% 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 %}

```auto

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/](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 |

```auto
I have probably to compleet the html file, I will look at it

thanks for your support
```

---

<div class="post-metadata">

**Author:** ![ealalin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ealalin/32/17520_2.png) [@ealalin](https://forum.djangoproject.com/u/ealalin)\
**Post date:** [January 13, 2024, 8:59pm UTC](https://forum.djangoproject.com/t/a-class-got-unexpected-keyword-argument/26916/4 "2024-01-13T20:59:38Z")

</div>

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

````auto
''' 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
````
