簡體   English   中英

使用主鍵索引 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})

這是我的測試1.html:

Hello World

{{ items }}

{% for item in items %}
        <a href="{% url 'current_games' item.pk %}">{{ item.home_team_field }}</a>
{% endfor %}

這是我的測試2.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”列表的索引?

您可以使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM