Josh
Josh

Reputation: 380

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? 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:

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:

<p>The price of this item is: {{ item }}</p>


Where is it?!!?

{{ item.uuup }}

URL file:

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. 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? 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?

After the update you gave here is the error I am getting:

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

Upvotes: 0

Views: 834

Answers (1)

Ahtisham
Ahtisham

Reputation: 10116

You can slice in your view like this:

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

Using pk as index is not right the reason being pk value is not consistent instead you can do something like this:

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

Upvotes: 1

Related Questions