0

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

4
  • 2
    You can use the render shortcut function docs.djangoproject.com/en/3.0/topics/http/shortcuts/#render Commented May 6, 2020 at 20:33
  • 1
    what is type of the request? If it is GET and you want to render the template then use render instead of HttpResponse. Something like this - return render(request, 'your_template.html', context=playerdata) Commented May 6, 2020 at 20:35
  • Wouldn't that redirect me to another page? Jeet Thanks ger.s.brett I'll take a look! Commented May 6, 2020 at 20:37
  • 1
    render doesn'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 use ajax call. you can refer this question for how to update a django template after an ajax call Commented May 6, 2020 at 20:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.