【问题标题】:Using Primary Key To Index HTML Template Django使用主键索引 HTML 模板 Django
【发布时间】: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


    【解决方案1】:

    您可以像这样在视图中切片:

    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})
    

    【讨论】:

    • 所以显示我表中的每个项目,我的问题是,我只是想在 /current_games/1 页面上显示 uuup[0] 和 /current_games/2 我想显示 uuup [1]。我添加了我的 URL 文件以防万一
    • 但是使用 pk 作为索引似乎不对。
    • 这很可能是真的,这是我如何到达这里的原始问题哈哈。 stackoverflow.com/questions/70763271/…。我发现自己很难掌握动态创建 html 页面的整个过程,此外,由于我没有为每个游戏创建表格,因此我如何在创建的每个页面上只显示一行甚至更好的 sql 表的一个单元格,我为所有游戏创建了一个表格,然后尝试将表格中的数据解析到每个视图中。我也不太擅长解释。
    • 检查我的更新答案
    • 感谢您帮助纠正这个问题,但现在我确实收到了一个错误,发布在原始问题中。
    猜你喜欢
    • 2016-06-16
    • 2013-01-26
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    • 2017-04-11
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多