Reputation: 25
At this point I just want to call a page based on my table.html template. I'll work on adding all of my table data later. Presently, when I click on my link to a table on the index.html page I get a "TypeError... table() got an unexpected keyword argument 'd_100_id'.
I've tried removing unnecessary code from the table view and template, and I checked in an incognito Chrome browser in case I'm getting the old page each time.
My views.py page
from django.shortcuts import get_object_or_404, render
from .models import D100Generator
def index(request):
latest_table_list = D100Generator.objects.order_by('-d_100_id')[:5]
context = {
'latest_table_list': latest_table_list
}
return render(request, 'generators/index.html', context)
def table(request, table_slug):
table = get_object_or_404(D100Generator, pk=table_slug)
return render(request, 'generators/table.html', {'table': table})
My url.py (in case this helps):
from . import views
app_name = "generators"
urlpatterns = [
path('', views.index, name='index'),
path('table/<slug:d_100_id>', views.table, name='table'),
]
index.html
<br>
<h2>Recent tables added include:</h2>
{% if latest_table_list %}
<ul>
{% for table in latest_table_list %}
<li><a href="/generators/table/{{ table.table_slug }}">{{ table.table_name }}</a></li>
{% endfor %}
</ul>
{% else %}
<p>No tables are available.</p>
{% endif %}
and table.html
<h1>{{ table.table_name }}</h1>
I wanted it to call at least the table.html page and display the table_name.
What I got instead was an error message at the top of the page:
TypeError at /generators/table/your-elf-spent-25-years-learning
table() got an unexpected keyword argument 'd_100_id'
Request Method: GET
Request URL: http://localhost:8000/generators/table/your-elf-spent-25-years-learning
Django Version: 2.2.6
Exception Type: TypeError
Exception Value:
table() got an unexpected keyword argument 'd_100_id'
Exception Location: C:\Users\tirli\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py in _get_response, line 113
Python Executable: C:\Users\tirli\AppData\Local\Programs\Python\Python37\python.exe
Python Version: 3.7.4
Python Path:
['C:\\Users\\tirli\\OneDrive\\Documents\\Schoolwork\\2020 Fall\\IT '
'4750\\Capstone\\capstone',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37\\Scripts',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37\\DLLs',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37\\Doc',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37\\include',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37\\libs',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37\\Tools',
'C:\\Users\\tirli\\OneDrive\\Documents\\Schoolwork\\2020 Fall\\IT '
'4750\\Capstone\\capstone',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37\\lib',
'C:\\Users\\tirli\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages']
Server time: Wed, 16 Oct 2019 17:29:47 -0600```
I can post the traceback as well if needed.
Upvotes: 1
Views: 152
Reputation: 2484
Your path should look like this:
path('table/<slug:table_slug>', views.table, name='table'),
def table(request, table_slug):
table = get_object_or_404(D100Generator, pk=table_slug)
return render(request, 'generators/table.html', {'table': table})
Also, in template, you can use url
template tag to reverse the URL rather than hardcoding it:
<li><a href="{% url 'generators:table' table_slug=table.pk %}">{{ table.table_name }}</a></li>
Upvotes: 1