Django-如何在Bootstrap下拉菜单中添加项目?

时间:2019-08-17 03:32:51

标签: python django bootstrap-4

在某些背景下,我使用Django和Bootstrap构建待办事项清单作为副项目。我遇到的问题是我无法将项目添加到下拉列表中。我希望有一个下拉列表,以列出人的名字,但是当我尝试将其添加到下拉列表中时,他们不会出现。

这是我的观点。如您所见,我以传递名称列表为例。

def index(request):

form = TodoForm()
people = ['person1', 'person2', 'person3']

if request.user.is_authenticated is False:
    return render(request, 'main/index.html', {'todo_list': [], 'form': form})

user = User.objects.get(pk=request.user.id)
todo_list = user.todo_set.all().order_by('id')
return render(request, 'main/index.html', {'todo_list': todo_list, 'form': form, 'people': people})

这是我从Bootstrap获得的下拉列表:

            <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Remind others
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                {% for person in people %}
                    <a class="dropdwon-item" href="#">{{ person }}</a>
                {% endfor %}
            </div>
        </div>

运行此代码时,我确实看到了下拉按钮,但是当我单击该按钮时,它下拉​​了,但没有显示任何名称。这只是一个空白的下拉列表。

我知道下拉列表可以工作,因为当我运行下面的代码时,它可以正常工作。

            <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                Remind others
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                <a class="dropdown-item" href="#"></a>
                <a class="dropdown-item" href="#">Another action</a>
                <a class="dropdown-item" href="#">Something else here</a>
            </div>
        </div>

感谢您的帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

您的代码看起来不错。然而 if request.user.is_authenticated is False:阻止people进入您的html页面。只需记住进行身份验证即可查看完整的下拉菜单:)