0

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())})
  • Which view renders this template and does it display data dynamically? Can you also share the `League` model? I think the url is fired for a particular instance(single instance), share html part properly. – Sunderam Dubey Oct 05 '22 at 05:43
  • the `def draft(request):` function renders the `draft` template, which is dynamic, it gets the league_id and league_title using the id. The model is just `League` that has a CharField for `name` (and `id` created automatically) – Kaden Miller Oct 05 '22 at 15:13

1 Answers1

0

You can add league_id and league_title dynamically like this:

$(document).ready(function(){  
    setInterval(function(){
      const league_id = '1' //You need to get these from DOM
      const league_title = 'foo bar'
      $.ajax({
          type:"GET",
          url: `/${league_id}/${league_title}/getPlayers/`,
          success: function(response){
              console.log(response);
          },
          error: function(response){
              alert("An error occurred")
          }
      });
  },1000);

})
enes islam
  • 1,054
  • 4
  • 12
  • I set `const league_id` and `const league_title` equal to `{{league.id}}` and `{{league.title}}`, however then I use `url: '/${league_id}/${league_title}/getPlayers/',` it just tries to go to `url: '/${league_id}/${league_title}/getPlayers/',`. It seems the `${}` isn't working? – Kaden Miller Oct 05 '22 at 15:09
  • You should use backticks instead of quotes. See [this](https://stackoverflow.com/a/35984254/15071244) answer for this – enes islam Oct 06 '22 at 06:39