[Answered ]-Using Len Of A List To Create Django HTML Pages?

1👍

What I think you want is called a detail page. I’m going to call the model Item. You can create detail views for each item easily by doing something like the following on your urls.py:

    # urls.py
    urlpatterns = [
        ...
        path('your_details_view/<int:pk>', views.your_details_view, name='your_details_view'),
        # or if your primary key is NOT an integer:
        path('your_details_view/<str:pk>', views.your_details_view, name='your_details_view'),
        ...
        ]

Your root urls.py looks different than I’m used to. Perhaps is is correct, but give this a try. Remove the app part, so like this:

    urlpatterns = [
        path('admin/', admin.site.urls),        
        path("", include("authentication.urls")),
        path("", include("home.urls")), 
    ]

Then the following in your views:


    # views.py

    # view of the page with all the links:
    def your_dynamic_table_view(request):
        items = Item.objects.all()
        # rest of your code
        return render(request, 'your_app/dynamic_table.html', {'items': items})

    # view for the details page
    def your_details_view(request, pk):
        # your code
        # Here is where you will get the particular item based 
        # on the pk that was in the <a href> link
        item = Item.objects.get(pk=pk)
        return render(request, 'your_app/detail_view.html', {'item': item})

Now, in your table you can put links to all items simply by iterating through them; something like this in your dynamic_table.html:

dynamic_view.html:

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

Now the link will go to your_details_view, which will render an html page specific to the pk of that item. (It doesn’t have to be a pk, or even an integer). The item.pk in the href anchor tag is so that when you click on it it will go to the your_details_view, which will render an html page, .../your_details_view/45 as an example, where 45 is the pk I made up for the particular item. That pk variable (the primary key of the particular item) can then be used in your detail_view.html page, along with any of the item’s fields:

detail_view.html:

    Here you have access to the all of the fields
    of the particular item, say item with pk=45, if
    that is pk in the <a href> tag.  For example if the
    item has a field called price, then you can do:
    <p>The price of this item is: {{ item.price }}</p>

Note I just made up the name item. For example, if you have a model you call Item, then in the template where you will have all the links (NOT the detail page), you can pass all the Item objects, and then iterate through them to create all the links.

0👍

The reason why you’re getting the below values all the same is because you’re sending the same list each time. Notice that you are NOT sending the same item, since you are using the pk to get the specific item. That’s why each link on the top gets you something specific.

def your_details_view(request, pk):

    # Here you are getting the SPECIFIC Nbav8 instance, and the links in your template,
    # <a href="{% url 'your_details_view' item.pk %}">{{ item }}</a>
    # the link will have that specific item's pk in the url, thus generating
    # a specific page to that item.
    item = Nbav8.objects.using('totals').get(pk=pk)

    # Everything beyond here looks like it's getting lists that do not depend 
    # on the specific item previously gotten.  
    current_day_home_team = list(Nbav8.objects.using('totals').values_list('home_team_field', flat=True))
    current_day_away_team = list(Nbav8.objects.using('totals').values_list('away_team_field', flat=True))

    awayuyu = []
    homeuyu = []

    for team in current_day_home_team:
        home_team_list1 = PreviousLossesNbav1WithDateAgg.objects.using('totals').filter(Q(away_team_field=team) | Q(home_team_field=team)).values_list('actual_over_under_result_field', flat=True)

        homeuyu.append(list(home_team_list1[:5]))


    home_team_list2 = homeuyu


    for team in current_day_away_team:
        away_team_list1 = PreviousLossesNbav1WithDateAgg.objects.using('totals').filter(Q(away_team_field=team) | Q(home_team_field=team)).values_list('actual_over_under_result_field', flat=True)
        away_teamdd = away_team_list1[:5]

        awayuyu.append(list(away_team_list1[:5]))


    away_team_list2 = awayuyu


    return render(request, 'home/testing2.html', {'item': item, 'away': away_team_list2, 'home': home_team_list2})

I’m not sure what specific list each Nbav8 object is supposed to get on it’s page, and my lack of sports knowledge makes it harder to grasp what you’re trying to show.

Suggestions:

Before going much further, there are some things you might want to address now, like

Not knowing your initial code, I made up a name your_details_view, but a more natural name might be current_game so that it matches up with your table of many current_game‘s.

Having names with spaces in the url is not such a great idea. Maybe you can add a field to your Nbav8 objects like url_name, and use that instead of pk in the anchor tag and path.

path('your_details_view/<str:url_name>', views.your_details_view, name='your_details_view'),

Then in your html

<a href="{% url 'your_details_view' item.url_name %}">{{ item }}</a>

Bottom Line

You have to figure out a way in you your_details_view to create a list that is specific to the item, so that when you send it to that item’s detail page, only that list will appear.

Leave a comment