I have a Django ajax template that will get all the Players from a players model:
<body>
<h1>List of Players:</h1>
<ul id="display-data">
</ul>
</body>
<script>
$(document).ready(function(){
setInterval(function(){
$.ajax({
type:"GET",
url: "{% url 'getPlayers' %}", # Here is where I think the problem is
success: function(response){
console.log(response);
},
error: function(response){
alert("An error occured")
}
});
},1000);
})
</script>
However my urls.py file for the template that I need to run this for is a dynamic url:
urlpatterns = [
path('<str:league_id>/<str:league_title>/draft/', draft, name='draft'),
path('<str:league_id>/<str:league_title>/getPlayers/', getPlayers, name="getPlayers"),
]
# Include url path is /league/
The problem is that the url set in the Ajax function is not including the league_id
and league_title
. Does anyone know how to add those parameters when setting the url path for the Ajax function? Or if that is even the problem with my setup?
Here is my views.py for the draft and the get:
def draft(request, league_id, league_title):
league = League.objects.get(id=league_id)
context = {'league': league}
return render(request, 'league/draft.html', context)
def getPlayers(request, league_id):
league = League.objects.get(id=league_id)
players = league.available_player.all()
return JsonResponse({'players': list(players.values())})