django - 如何将 slug 传递给 Django 中的 URL?
问题描述
此时我只想调用一个基于我的 table.html 模板的页面。稍后我将致力于添加我的所有表格数据。目前,当我单击 index.html 页面上的表链接时,我得到一个“TypeError ... table() 有一个意外的关键字参数‘d_100_id’。
我已经尝试从表格视图和模板中删除不必要的代码,并且我检查了一个隐身 Chrome 浏览器,以防我每次都得到旧页面。
我的views.py页面
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})
我的 url.py (如果这有帮助):
from . import views
app_name = "generators"
urlpatterns = [
path('', views.index, name='index'),
path('table/<slug:d_100_id>', views.table, name='table'),
]
索引.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 %}
和 table.html
<h1>{{ table.table_name }}</h1>
我希望它至少调用 table.html 页面并显示 table_name。
我得到的是页面顶部的错误消息:
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.
解决方案
您的路径应如下所示:
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})
此外,在模板中,您可以使用url
模板标签来反转 URL,而不是对其进行硬编码:
<li><a href="{% url 'generators:table' table_slug=table.pk %}">{{ table.table_name }}</a></li>
推荐阅读
- javascript - Atom-editor 自动生成 .tags/.tags1
- bash - 从嵌套函数停止脚本
- python - Python Docstring 如何在文档字符串中多行编写列表?
- python-2.7 - 升级 pyopenssl ImportError 后出现相同的错误:没有名为 _util 的模块
- android - 是否可以在 Android 应用程序中打开 SWF 文件?
- javascript - transaction_details.gross_amount 不是数字,即使 API 请求输入值,如何将其更改为数字?
- angular - 发布联系信息时出现 Graphql 错误
- c# - 如何解决 HTTP 错误 502.5 - 在asp.netcore 下使用Topshelf 创建windows 服务时进程失败?
- visual-studio - TFS Visual Studio 构建任务在构建后事件期间无法识别宏 DevEnvDir
- r - 使用 mutate 时函数在管道中不起作用