I am relearning django/python recently and trying to build an app to manage a season ticket draft between friends. I've become stuck while searching for this answer, could be my phrasing, but I'm trying to understand how to pass a value from one template to another. Here is an example of what I am doing.
views.py
def home_page_view(request):
sessions = DraftSession.objects.filter(session_status="ACTIVE")
return render(request, "index/index.html", {
'arena_mappings': ARENA_MAPPINGS,
'team_mappings': TEAM_MAPPINGS,
'sessions': sessions,
})
index.html
{% for session in sessions %}
<tr>
<td> {{ session.season_year }} </td>
<td> {{ team_mappings|get_item:session.home_team }} </td>
<td> {{ arena_mappings|get_item:session.home_team }} </td>
<td> {{ session.create_date }} </td>
<td> {{ session.session_owner }} </td>
<td> <button type="button" >Join Now!</button> </td>
</tr>
{% endfor %}
Using the button in the last column I want the user to be able to go to a new page that has awareness of the row that was selected, my assumption is this would be passed in via the pk for the db row. I've setup the view, urls, etc; just lost on how to pass the selection on the the new page. Appreciate any help!
CodePudding user response:
You are correct in your assumption. You just need to specify the url name in your href and pass in the parameter using the url template tag.
<td><a href=“{% url ‘session-view’ session.id %}” class=“btn btn-primary”>Join now!</a></td>
CodePudding user response:
<a href="/your-url/{{ session.pk }}" >Join Now!</a>