如何在django admin中要求用户输入操作? [英] How can I ask for user input with an action in django admin?

查看:280
本文介绍了如何在django admin中要求用户输入操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我正在编写一个用于分组的操作,我想问用户每个组他们想要多少人,然后用一个警告框进行响应,该警告框显示您有4个组,根据用户输入。我该如何在django管理员中执行此操作,如何创建某种弹出式窗口,以询问他们想放入一个小组中的人数? (我正在尝试通过采取行动来实现这一目标)

In my code, I am writing an action for grouping, I would like to ask the user how many people would they like per group and then respond with an alert box that says something along the lines of you have 4 groups, based on user input. How do I do this in django admin, how do I create some kind of pop up that asks for the amount of people that they would like to put in a group? (I'm trying to achieve this with an action)

admin.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类似:

admin.py Something like:

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包含以下内容:

File "admin/amount_per_form.html" contains something like:

 {% 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 admin中要求用户输入操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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