如何对Djang中的选定对象执行操作

2024-09-30 03:23:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Django的新手,在对数据库对象执行操作时遇到一些问题,我需要使用复选框进行选择
型号.py

    class Scooter (models.Model):
        name = models.CharField(max_length=100, verbose_name='name')
        price = models.IntegerField(max_length=10, verbose_name='price')
        url = models.URLField(verbose_name='url')

视图.py

    class MainView(TemplateView):
        template_name = 'mainApp/index.html'
        def get(self, request):
            scooters = Scooter.objects.all().order_by('price')
            return (render(request, self.template_name, context={'scooters':scooters}))

我的html模板是:

<table class="table table-sm table-borderless table-hover table-responsive-lg">
        <caption>Перечень товаров</caption>
        <thead class="thead-dark">
            <tr>
                <th scope="col"></th>
                <th scope="col">№</th>
                <th scope="col">Name</th>
                <th scope="col">Price</th>
                <th scope="col"></th>
                <th scope="col"></th>
            </tr>
        </thead>
        <tbody>
            <form action="" method="post">
                {% for elem in scooters %}
                <tr>
                    <th>
                        <input type="checkbox" id="{{ elem.num }}" name="scooter_select" value="/{{ elem.num }}">
                        <label for="scooter{{ elem.num }}"></label>
                    </th>
                    <th>
                        {{ forloop.counter }}
                    </th>
                    <th scope="row">
                        <a href="{{ elem.url }}">{{ elem.name }}</a>
                    </th>
                    <td>{{ elem.price }}</td>
                    <td>
                        <a href="{{ elem.id }}/update" class="badge badge-warning" role="button" aria-pressed="true">Edit</a>
                    </td>
                    <td>
                        <a href="{{ elem.id }}/delete" class="badge badge-danger" role="button" aria-pressed="true">Delete</a>
                    </td>
                </tr>
                {% endfor %}
            </form>
        </tbody>
    </table>

所以我不明白,我怎样才能抓住选中的复选框对象


Tags: namebadgeurlverbosemodelstablecolprice

热门问题