Charles
Charles

Reputation: 641

Passing a dictionnary in a template in Django

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

Upvotes: 0

Views: 44

Answers (1)

Hybrid
Hybrid

Reputation: 7049

The three ways to do this are:

1) Use the render() shortcut to map context to HTML

2) Use AJAX to get a python dict/json response, and merge it into html using JS

3) You can combine the two, and use AJAX/JS to get a render() response back from the server which is merged with context, and then append it to the DOM.

For option 1 you would use the return render(request, 'template.html', context) shortcut, and that would merge all the context variables to your template wherever {{ variable }} is present.

For option 2 you can use either jQuery ($.ajax({})) or vanilla JS (fetch()) to get back a JSONResponse, parse it in JS, and then merge the values individually back into your HTML where you see fit.

For option 3 you would again use either jQuery or vanilla JS, but this time your response would be context merged and rendered via render(), and returned as a whole section of a page which you can then append anywhere on the page you want.

Upvotes: 1

Related Questions