使用主键索引 HTML 模板 Django
Using Primary Key To Index HTML Template Django
我对如何根据 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 }}
URL 文件:
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 %}
您可以像这样切入视图:
def current_games(request, pk):
item = Nbav8.objects.using('totals').get(pk=pk)
index = pk - 1
items2 = CurrentDayStats.objects.using('totals').values_list('home_team_over_under_field', flat=True)[index]
return render(request, 'home/testing2.html', {'item': item, 'uuup':items2})
使用 pk 作为索引是不正确的,因为 pk 值不一致,您可以这样做:
testing1.html:
{% for item in items %}
<a href="{% url 'current_games' item.pk forloop.counter0 %}">{{ item.home_team_field }}</a>
{% endfor %}
urls.py:
path('current_games/<int:pk>/<int:page>/', views.current_games, name='current_games')
views.py:
def current_games(request, pk, page):
item = Nbav8.objects.using('totals').get(pk=pk)
items2 = CurrentDayStats.objects.using('totals').values_list('home_team_over_under_field', flat=True)[page]
return render(request, 'home/testing2.html', {'item': item, 'uuup':items2})
我对如何根据 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 }}
URL 文件:
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 %}
您可以像这样切入视图:
def current_games(request, pk):
item = Nbav8.objects.using('totals').get(pk=pk)
index = pk - 1
items2 = CurrentDayStats.objects.using('totals').values_list('home_team_over_under_field', flat=True)[index]
return render(request, 'home/testing2.html', {'item': item, 'uuup':items2})
使用 pk 作为索引是不正确的,因为 pk 值不一致,您可以这样做:
testing1.html:
{% for item in items %}
<a href="{% url 'current_games' item.pk forloop.counter0 %}">{{ item.home_team_field }}</a>
{% endfor %}
urls.py:
path('current_games/<int:pk>/<int:page>/', views.current_games, name='current_games')
views.py:
def current_games(request, pk, page):
item = Nbav8.objects.using('totals').get(pk=pk)
items2 = CurrentDayStats.objects.using('totals').values_list('home_team_over_under_field', flat=True)[page]
return render(request, 'home/testing2.html', {'item': item, 'uuup':items2})