【发布时间】:2022-01-26 04:25:57
【问题描述】:
对如何根据 HTML 模板中的主键索引列表感到困惑? 这是我的观点页面:
def current_game_table(request):
items = list(Nbav8.objects.using('totals').all())
return render(request, 'home/testing.html', {'items': items})
def current_games(request, pk):
item = Nbav8.objects.using('totals').get(pk=pk)
items2 = list(CurrentDayStats.objects.using('totals').values_list('home_team_over_under_field', flat=True))
return render(request, 'home/testing2.html', {'item': item, 'uuup':items2})
这是我的 testing1.html:
Hello World
{{ items }}
{% for item in items %}
<a href="{% url 'current_games' item.pk %}">{{ item.home_team_field }}</a>
{% endfor %}
这是我的 testing2.html:
<p>The price of this item is: {{ item }}</p>
Where is it?!!?
{{ item.uuup }}
网址文件:
from django.urls import path, re_path
from apps.home import views
urlpatterns = [
# The home page
#path('', views.index, name='home'),
# Matches any html file
#path('charttest/', views.charttest, name='charts'),
path('', views.nba, name='nba'),
path('nbav2/', views.nba2, name='nba2'),
path('nbav3/', views.nba3, name='nba3'),
path('ncaa/', views.ncaa, name='ncaa'),
path('nhl/', views.nhl, name='nhl'),
path('testing/', views.current_game_table, name='testing'),
path('current_games/<int:pk>', views.current_games, name='current_games')
我的页面显示良好 testing1 根据我的需要显示我的页面以进行测试,我的问题是这样的。在显示的 testing2.html 页面上,我有一个列表“uuup”,我的问题是,我只想在第 1 页显示 uuup.0,在第 2 页显示 uuup.1,在第 3 页显示 uuup.2,等等。我似乎无法弄清楚如何使用 pk 作为索引?我在我的视图页面中设置了一个 diff 变量并将其设置为 int(item) 并在每个页面上打印出来以确认每个子页面将其显示为 1/2/3/4/etc 所以我不确定如何调用它对我的“uuup”列表使用索引?
在你在这里给出的更新之后是我得到的错误:
Reverse for 'current_games' with arguments '(1, 0)' not found. 1 pattern(s) tried: ['current_games/(?P<pk>[0-9]+)$']
1 Hello World
2
3 {{ items }}
4
5 {% for item in items %}
6 <a href="{% url 'current_games' item.pk forloop.counter0 %}">{{ item.home_team_field }}</a>
7 {% endfor %}
【问题讨论】:
标签: python django django-views django-templates