Generate data when button/link is clicked Django


I’m very new to Django and web apps. I have an HTML page that allows users to search database for player names. An HTML table is returned. Each entry is a player’s performance in relation to a game they played. Each game has 10 players associated with it


<h1>Search Results for: {{searched}}</h1>
            {% for player in match %}
                  <a href=search_player_db/{{player.match_id}}>{{player.match_id}}</a> 
                <td>{{ player.role}}</td>
                <td>{{ player.win_or_loss}}</td>
            {% endfor %}

The match id is a link that brings the user to a page with additional details related to the match

{%block stats%}
    <h1>Winning team</h1>
        {%for player in winners %}
    <h1>Losing team</h1>
        {%for player in losers %}

Instead of being redirected to a new page when clicking the link, I’d like to load the content from match_details.html into the page below the table on search_db.html through the button/link in search_db.html. That way the user can click through search results

    def search_playerdb(request):
        if request.method == "POST":
            searched = request.POST['searched']
            players = PlayerInfo.objects.filter(player_name__contains=searched)
            'searched': searched, 
            'players': players}
            return render(request, 'searchdb.html', context)

            return render(request, 'searchdb.html', {})

    def display_match(request, matchid):
        match = PlayerInfo.objects.filter(match_id=matchid)
        winners = match.filter(win_or_loss=True)
        losers = match.filter(win_or_loss=False)
        context = {
            'match': match,
            'winners': winners,
            'losers': losers,}
        return render(request, 'match_details.html', context)


In order to do this you’ll need to use Javascript to make an AJAX call. In case you’re unaware, an AJAX call allows a web page to send or receive data without having to refresh the page.

This can be done using pure javascript – Example

Or you could use a library to abstract some of the complexity away. One example of this would be JQuery

In either case, you will be calling a new URL on your site.

Answered By – Mat

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published