[英]Using Primary Key To Index HTML Template Django
Bit confused on how I would index a list I have based on the primary key in the HTML template?对如何根据 HTML 模板中的主键索引我拥有的列表有点困惑? Here is my views page:
这是我的观点页面:
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})
Here is my testing1.html:这是我的测试1.html:
Hello World
{{ items }}
{% for item in items %}
<a href="{% url 'current_games' item.pk %}">{{ item.home_team_field }}</a>
{% endfor %}
Here is my testing2.html:这是我的测试2.html:
<p>The price of this item is: {{ item }}</p>
Where is it?!!?
{{ item.uuup }}
URL file: 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')
My pages display fine testing1 shows my pages as I want for testing purposes, my problem is this.我的页面显示良好 testing1 根据我的需要显示我的页面以用于测试目的,我的问题是这个。 On my testing2.html page that displays, I have a list "uuup", my problem is, I only want to show uuup.0 on page 1, uuup.1 on page 2, uuup.2 on page 3, etc. I can't seem to figure out how to use the pk as an index?
在我的 testing2.html 页面上显示,我有一个列表“uuup”,我的问题是,我只想在第 1 页显示 uuup.0,在第 2 页显示 uuup.1,在第 3 页显示 uuup.2,等等。我似乎无法弄清楚如何使用 pk 作为索引? I set a diff variable in my views page and set it to int(item) and printed it out on each page to confirm that each subpage showed it as 1/2/3/4/etc so I am unsure how to call it to use an index to my "uuup" list?
我在我的视图页面中设置了一个 diff 变量并将其设置为 int(item) 并在每个页面上打印出来以确认每个子页面将其显示为 1/2/3/4/etc 所以我不确定如何调用它使用我的“uuup”列表的索引?
You can enumerate in template using forloop counter like this:您可以使用 forloop 计数器在模板中枚举,如下所示:
<p>The price of this item is: {{ item }}</p>
Where is it?!!?
{% for up in uuup %}
<p> {{ forloop.counter0 }} -> {{ up }} </p>
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.