1👍
You can use your urls.py to pass a variables through via the URL notation.
For example – Getting Team info:
template
{% for team in youngest_teams %}
<a href="{% url 'get_team_info' team.id %}">Team</a>
{% endfor %}
url.py
path('team_info/<team_id>', view=team_info, name="get_team_info")
views.py
def team_info(request, team_id=None):
team = Team.objects.get(id=team_id) #Information on team is passed.
0👍
There are multiple cases in your case. Let me explain them one by one.
Let’s say you have two templates. One is template 1 and the other is template 2.
- If you want to use the same logic for template 1 and template 2. You can use a variable in the views.py & url.py and use a condition there on that variable which is team_id in your case. E.g.
def index(request, team_id):
list_teams = Team.objects.filter(team_level__exact="U09")
context = {'youngest_teams': list_teams}
if team_id == 1:
return render(request, '/best/index.html', context)
elif: team_id == 2:
return render(request, '/best/template2.html', context)
And in urls.py, you can write:
path('team_info/<int:team_id>', view=team_info, name="get_team_info")
- If you don’t want the variable in the views and URLs. Then you can write different views with the same code in views.py. But in the case of multiple ids, it will cause code duplication.
- [Answered ]-Django ORM: Get latest record for distinct field
- [Answered ]-Prevent a list of names from splitting in a line break in a Django template
Source:stackexchange.com