'view.py got an unexpected keyword argument topic_id #p
I'm trying to fix this view keep getting this error message,
'''
Exception Type: TypeError
Exception Value:
topics() got an unexpected keyword argument 'topic_id'
Exception Location: /home/draco/shola/learning_log/lib/python3.8/site-packages/django/core/handlers/base.py, line 181, in _get_response
'''
view.py
def topics(request):
topics = TopicModel.objects.order_by('date_added')
context = {'topics' : topics}
return render(request, 'learning_logs/topics.html',context)
def topic(request, topic_id):
topic = TopicModel.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)
urls.py
url(r'^$', views.index, name='index.html'),
url(r'^topics/$',views.topics, name='topics.html'),
url(r'^topics/(?P<topic_id>\d+)\$',views.topics,name='topic.html')
models.py
from django.db import models
class Topic(models.Model):
text = models.CharField(max_length=200)
date_added = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.text
class Entry(models.Model):
topic = models.ForeignKey(Topic,
on_delete = models.DO_NOTHING)
text = models.TextField()
date_added = models.DateTimeField(auto_now_add=True)
class Meta:
verbose_name_plural = 'entries'
def __str__(self):
if len(self.text) >50:
return self.text[:50]+"....."
else:
return self.text
Template
<!DOCTYPE html>
{% extends "learning_logs/base.html" %}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% block content%}
<p>Topics</p>
<ul>
{%for topic in topics%}
<li>
<a href="{% url 'learning_logs:topic.html' topic.id %}">{{topic}}</a>
</li>
{%empty%}
<li>no topic have been added yet</li>
{%endfor%}
</ul>
{%endblock content%}
</body>
</html>
Solution 1:[1]
Error in this line of code:
url(r'^topics/(?P<topic_id>\d+)\$',views.topics,name='topic.html')
url(r'^topics/(?P<topic_id>\d+)\$',**views.topics**,name='topic.html')
should be corrected for
url(r'^topics/(?P<topic_id>\d+)\$',**views.topic**,name='topic.html')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ouflak |
