gpt4 book ai didi

django - 如何通过 django admin 中的操作请求用户输入?

转载 作者:行者123 更新时间:2023-12-03 21:35:13 25 4
gpt4 key购买 nike

在我的代码中,我正在编写一个用于分组的操作,我想询问用户他们希望每个组有多少人,然后用一个警告框进行响应,根据用户输入,您有 4 个组.我如何在 django admin 中执行此操作,我如何创建某种弹出窗口,询问他们想要加入一个组的人数? (我试图通过一个 Action 来实现这一点)

管理.py:

 Def howmany (modeladmin, request, queryset):
people = queryset.count()
amount_per = [the number that the user inputs]
Amount_of_groups = people/amount_per

最佳答案

admin.py 类似:

Class MyAdmin(admin.ModelAdmin):

def howmany (modeladmin, request, queryset):
people = queryset.count()
amount_per = [the number that the user inputs]
Amount_of_groups = people/amount_per

if 'apply' in request.POST:
form = AmountPerForm(request.POST)

if form.is_valid():
amount_per = form.cleaned_data['amount_per']
self.message_user(request, u'You selected - %s' % amount_per)
return HttpResponseRedirect(request.get_full_path())
else:
form = AmountPerForm()

return render(request, 'admin/amount_per_form.html', {
'items': queryset.order_by('pk'),
'form': form,
'title': u'Your title'
})

文件“admin/amount_per_form.html”包含如下内容:
 {% extends 'admin/base_site.html' %}

{% block content %}
<form action="" method="post">
{% csrf_token %}
<input type="hidden" name="action" value="assign_new_manager" />
{{ form }}
<p>Apply for:</p>
<ul>{{ items|unordered_list }}</ul>
<input type="submit" name="apply" value="Apply" />
</form>
{% endblock %}

关于django - 如何通过 django admin 中的操作请求用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25233840/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com