如何在Django表单中实现下拉列表?

时间:2019-03-04 04:46:59

标签: python django

我正在尝试在Django中实现一个下拉列表,其中包含其域与当前登录用户的域匹配的那些用户的电子邮件地址。

我正在使用以下代码,但它仍显示一个列表,而不显示下拉列表。我尝试将小部件部分从ChoiceField更改为MultipleChoiceField,现在更改为CheckboxSelectMultiple,只是为了查看是否显示了复选框,但似乎都不起作用。

这是我的 forms.py

class AssignTask(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.CharField(widget=forms.Textarea)
    assign_to = forms.MultipleChoiceField(choices=[], widget=forms.CheckboxSelectMultiple, required=False)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        user_email = self.user.email.split('@')[1]
        super(AssignTask, self).__init__(*args, **kwargs)
        self.fields['assign_to'] = forms.MultipleChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])

这是我的 views.py

@login_required
def assigntask(request):
    assign_form = AssignTask(user=request.user)
    return render(request, 'todoapp/assign_task.html', context={'assign': assign_form})

这是我的 html

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

{% block title %}Create a task{% endblock %}

{% block content %}
    <h2>Create a task and assign it to a user</h2>
    <form method="post">
        {% csrf_token %}
        {{ assign.as_p }}
        <br/><input type="submit" value="Assign">
        &nbsp;&nbsp;<button onclick="location.href='{%url 'dashboard' %}'" type="button">Go back</button>
    </form>
{% endblock %}

显然,该解决方案对我有用

class AssignTask(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.CharField(widget=forms.Textarea)
    assign_to = forms.ChoiceField(widget=forms.Select(choices=[]))

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        user_email = self.user.email.split('@')[1]
        super(AssignTask, self).__init__(*args, **kwargs)
        self.fields['assign_to'] = forms.ChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])

1 个答案:

答案 0 :(得分:1)

如果我理解正确,可以使用此代码解决问题吗?

forms.py

class AssignTask(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.CharField(widget=forms.Textarea)
    # assign_to = forms.MultipleChoiceField(choices=[], widget=forms.CheckboxSelectMultiple, required=False)

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        user_email = self.user.email.split('@')[1]
        super(AssignTask, self).__init__(*args, **kwargs)
        # self.fields['assign_to'] = forms.MultipleChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])
        self.fields['assign_to'] = forms.ChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])

您可以看到一个下拉框吗?这就是你的意思吗?

我想您可能忘记了在__init__函数中进行更改。


↓添加了--------

重要的是__init__内部。 您首先声明的“ assign_to”将在__init__部分中被覆盖。 例如,假设您将“ assign_to”声明为URLField,则ChoiceField将覆盖它。我要解释的是下面的

forms.py

class AssignTask(forms.Form):
    title = forms.CharField(max_length=200)
    description = forms.CharField(widget=forms.Textarea)
    assign_to = forms.URLField()

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        user_email = self.user.email.split('@')[1]
        super(AssignTask, self).__init__(*args, **kwargs)
        print(self.fields)
        self.fields['assign_to'] = forms.ChoiceField(choices=[(i.email, i.email) for i in User.objects.filter(is_active=True, email__icontains=user_email)])
        print(self.fields)

输出

OrderedDict([('title', <django.forms.fields.CharField object at 0x0000000004D90C88>), ('description', <django.forms.fields.CharField object at 0x0000000004D90CC0>), ('assign_to', <django.forms.fields.URLField object at 0x0000000004D90668>)])
OrderedDict([('title', <django.forms.fields.CharField object at 0x0000000004D90C88>), ('description', <django.forms.fields.CharField object at 0x0000000004D90CC0>), ('assign_to', <django.forms.fields.ChoiceField object at 0x0000000004D906A0>)])

仍然显示一个下拉框,不是吗?