I need to pass a dictionnary which I generate in my views.py to my template in Django.
template
{% for server in servers %} <div class="hs-item set-bg" data-setbg="{% static 'img/slider-1.jpg' %}"> <div class="hs-text"> <div class="container"> <h2>The Best <span>Games</span> Out There</h2> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec malesuada <br> lorem maximus mauris scelerisque, at rutrum nulla dictum. Ut ac ligula sapien. <br>Suspendisse cursus faucibus finibus.</p> <a href="#" class="site-btn">Read More</a> </div> </div> </div> {% endfor %} views.py
def get_data(request, server_name): server=Server.objects.get(Name=server_name) address = (server.IP, server.Port) connect = "steam://connect/"+str(server.IP)+":"+str(server.Port) queryset=a2s.info(address, timeout=3.0, encoding=None) max_player=queryset.max_players current_player=queryset.player_count current_map=queryset.map_name playerdata={ 'map': current_map, 'players': current_player, 'adress': connect, 'max': max_player, } return HttpResponse(playerdata) models.py
class Server(models.Model): Name = models.CharField(max_length=100) IP = models.TextField() Port = models.IntegerField() Image = models.ImageField(default='default.jpg', upload_to='server_pics') def __str__(self): return str(self.Name) if self.Name else '' What I'm trying to do here, is to loop over all the servers in my template, then, for each server, query said server using a python script, putting the value in a dictionnary, i.e player_count, current_map, etc, then displaying those informations in my template.
My problem is that, i don't know how to return said dictionnary to the template. I've already done something similar by using ChartJS, using JsonResponse in my view. However, I don't know how to return the dictionnary here, since I'm not using JS here.
Thanks in advance for your help
GETand you want to render the template then userenderinstead ofHttpResponse. Something like this - return render(request, 'your_template.html', context=playerdata)renderdoesn't redirects. It renders the template. If you mean, will it refresh the whole page, then yes. If you want to load it without refreshing the whole page then please useajaxcall. you can refer this question for how to update a django template after an ajax call