【问题标题】:Show Groupnames in a dropdown list在下拉列表中显示组名
【发布时间】:2020-12-29 13:44:18
【问题描述】:

我正在使用 Python 和 Django 开发一个 studentresult 网站。现在我想要求数据库给我数据库中组的名称。使用来自 django 的标准 db.sqlite3 数据库。在下拉菜单中,我得到三个白条,因为我现在有三个组。当我将 Class DisplayGroups 更改为 id = Models.IntegerField(id, flat=True) 将 return.self.group_name 更改为 return self.id 时,下拉列表会显示该组的 ID。但是我怎样才能显示该组的名称。尝试了很多:

  • 在模型中改为分组
  • 在模型中改为组名
  • 更改了 views.py 中的一些命令
  • 创建了一个新的数据库项 Groepen 并将查询更改为该项。

views.py

from django.shortcuts import render, redirect
from .models import DisplayGroups, DisplayUsername
from django.contrib.auth.models import User, Group

def index(request):
    response = redirect('/login')
    return response


def home(response):
    return render(response, "home/home.html")


def bekijk(request):
    DisplayGroup = Group.objects.all()
    print(DisplayGroup)
    DisplayNames = User.objects.all()
    print(DisplayNames)
    return render(request, "home/bekijk.html", {"DisplayGroups": DisplayGroup,"DisplayUsername":DisplayNames})

models.py

from django.db import models


# Create your models here.

class DisplayGroups(models.Model):
    group_name = models.CharField(max_length=200)

    def __str__(self):
        return self.group_name

class DisplayUsername(models.Model):
    username = models.CharField(max_length=100)

    def __str__(self):
        return self.username

html 页面

{% extends 'home/base.html' %}

{% block title %}Cijfers studenten{% endblock %}

{% block javascript %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
    $(document).ready(function()
    {
        var $productvar=$("#groupdd1");
            $itemvar=$("#userdd1");

            $options=$itemvar.find('option');
            $productvar.on('change', function()
            {
                $itemvar.html($options.filter('[value="'+this.value+'"]'))
            }).trigger('change');
    });
</script>
{% endblock %}
{% block content %}
<h1>Studentresultaten</h1>
<select id="groupdd1">
    <option disabled="true" selected>-- Select Fruit --</option>
    {% for result in DisplayGroups %}
    <option>{{result.group_name}}</option>
    {% endfor %}
</select>
<select id="userdd1">
    <option disabled="true" selected>-- Select Fruit --</option>
    {% for result in DisplayUsername %}
    <option>{{result.username}}</option>
    {% endfor %}
</select>
{% endblock %}

jquery 是以后的,因为我想做一个依赖的下拉菜单。希望你们能帮助我。提前致谢!

【问题讨论】:

  • 您查询DisplayGroup = Group.objects.all(),但您的模型是DisplayGroups
  • @WillemVanOnsem 当我将查询更改为DisplayGroups = Group.objects.all() 时,下拉菜单仍显示三个条目,但全为空白。字符串未显示。
  • 当然,因为Group 没有group_name 字段,所以{{ result.group_name }} 没有意义。
  • 但它有一个名称,在本例中为 test。是否可以在下拉菜单中显示该名称,还是不能这样做?
  • 只需使用{{ result }}。然后它会调用群组上的str(..),或者你可以使用{{ result.name }}

标签: python django


【解决方案1】:

@WilliamVanOnsem 是正确的,您从中过滤的模型是 Group,它没有 group_name 作为字段。这是你可以做的。 在views.py中

DisplayGroup = DisplayGroups.objects.all()
DisplayNames = DisplayName.objects.all()

现在在 HTML 模板中您可以访问 group_name 和 result.username。用户模型有用户名字段,所以 result.username 没有显示空文本

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 2014-06-05
    • 2017-02-04
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    相关资源
    最近更新 更多