【问题标题】:How can I ask for user input with an action in django admin?如何在 django admin 中通过操作请求用户输入?
【发布时间】:2014-10-03 17:23:12
【问题描述】:

在我的代码中,我正在编写一个用于分组的操作,我想问用户他们希望每个组有多少人,然后用一个警告框回复,上面写着你有 4 个组,基于关于用户输入。我如何在 django admin 中执行此操作,如何创建某种弹出窗口,询问他们想加入一个组的人数? (我正在尝试通过一个动作来实现)

admin.py:

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

【问题讨论】:

    标签: django django-models django-admin-filters


    【解决方案1】:

    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" />
        <input type="hidden" name="_selected_action" value="{{ item.pk }}"/> {# thanks to @David Hopkins for highligting it #}
    
        {{ form }}
        <p>Apply for:</p>
        <ul>{{ items|unordered_list }}</ul>
        <input type="submit" name="apply" value="Apply" />
     </form>
     {% endblock %}
    

    【讨论】:

    • 请注意,在表单中遍历项目列表时,您需要包含隐藏的 _selected_action 字段。如果没有它,当您提交时,您将不会被带到您的操作中,只会返回到管理员列表,并且您的操作将不会成功(使用 Django 版本 2.2)。示例:....我只是浪费了一个小时使用这个解决方案?
    • 太棒了!感谢您的建议,我遇到了完全相同的问题!
    【解决方案2】:

    我找到了一个更简化更好的方法here

    您只需要像这样创建一个操作表单。

    from django.contrib.admin.helpers import ActionForm
    from django import forms
    
    
    class XForm(ActionForm):
        x_field = forms.ModelChoiceField(queryset=Status.objects.all(), required=False)
    

    现在,在 admin.py 中定义 XForm

    class ConsignmentAdmin(admin.ModelAdmin):
    
        action_form = XForm
        actions = ['change_status']
    
        def change_status(modeladmin, request, queryset):
            print(request.POST['x_field'])
            for obj in queryset:
                print(obj)
        change_status.short_description = "Change status according to the field"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 2022-11-24
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      相关资源
      最近更新 更多