Django显示模型对象的多个下拉列表 [英] Django multiple dropdowns showing model objects

查看:105
本文介绍了Django显示模型对象的多个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有多个下拉菜单,每个下拉菜单都显示一个特定模型中的可用对象。

I want to have multiple dropdowns, each showing the available objects from one specific model.

所以下拉菜单1:

Apples
Oranges
Pears

和下拉列表2:

Apples
Oranges
Pears

等。

奖金问题是将这些下拉列表链接/

The bonus question is to have these dropdowns linked/be dependent so that as the user selects items, these chosen items are removed from the remaining dropdowns.

这可能吗?

推荐答案

有可能。将模型导入视图文件。
示例:

It is possible. Import your Model to the view-File. Example:

def editUser(request):
    users = "YourModel".objects.all()

    if request.method="POST":
       selected_Item = request.POST['user.id']
       userID = Employee.objects.get(id=selected_item)
       userID.delete()


    context = {
             'user' : users
    }

    return render(request, "your_template_name", context)

因此,您可以通过ID或名称来选择商品。在您的模板中,您可以说 user.your_stuff。因此,如果您的模型具有名称之类的内容,则可以编写user.name。
然后删除内容。

So you select your Item by your ID or name. In your Templates you can say "user.your_stuff". So if your Model has things like name you can write user.name. Then delete the stuff.

上下文层次结构就像字典一样。您可以在模板中使用它。

Context hier is like a Dictonary. You can work with it in your Template.

<form method="POST" > {%csrf_token%}
<select name="user.id">
{% for entry in user %}
    <option>  {{ entry.id }} </option>
{% endfor %}
</select>

<input type = "submit" value="Submit">
</form>

因此,现在您有了一个下拉菜单,其中列出了用户的所有条目。
您可以在视图中编辑退货,因此只需调用同一页面,您就可以刷新网站,而您想要删除的值就消失了。

So now you have a DropDown Menu that lists all Entrys from user. You can edit your return in your View so just call the same page and you just "refreshed" the Site and the value you want to delete is away.

对我英语不好或解释不好对不起。我仍然在提高我的英语技能,我也提高了自己的英语水平StackOverflow和Django:P
如果您还有任何疑问,我可以在这里帮助您! :)

Im sorry for my bad english or for my bad explanation. Im still improving my English Skills and im new too StackOverflow and Django too :P If you have any Questions left, im here to help! :)

这篇关于Django显示模型对象的多个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆