S
S
skoomer2017-06-07 12:58:40
Django
skoomer, 2017-06-07 12:58:40

How to add model to django template table?

hello everyone, I have a django model, defined it in the view, registered it in the admin panel, now I want (to display it in the table) connect it to the table (the simplest one) how to implement it?
so far it turns out like on the skin below
models.py

from django.db import models

class GameServer(models.Model):
    name = models.CharField(max_length=30)
    voic = models.CharField(max_length=50)
    rate = models.CharField(max_length=60)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ["name"]

I defined it in views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.shortcuts import render_to_response

from l2.models import GameServer


def l2(request):

    game_servers_filters = request.GET.getlist("game_servers_filters")
    kwargs = dict()

    game_servers = GameServer.objects.filter()  # Список всех серверов

    if game_servers_filters:
        kwargs["id__in"] = game_servers_filters
        game_servers = GameServer.objects.filter()


    return render(request, 'l2/l2.html', {'game_servers': game_servers})

<code lang="html">
<!DOCTYPE html>
{% load staticfiles %}
{% load static %}

<html lang="en">
<head>
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">

    <meta charset="UTF-8">


        <title>{% block title %}{% endblock %}</title>
    </head>
    <body>

{% include 'navbar.html' %}


        <div>{% block content %}<form method="get" class="form-horizontal">
                        <div class="form-group">
                            <select multiple class="form-control" name="game_servers_filters">
                                {% for item in game_servers %}
                                    <option value="{{ item.id }}">{{ item.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </form>{% endblock %}</div>

    </body>
</html>
</code>
cbcf1cec972941c0b888a71a66a1e13d.jpg

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question