Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ask for user input with an action in django admin?

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
like image 379
user3806832 Avatar asked Aug 10 '14 23:08

user3806832


1 Answers

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'
            })

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" />
    {% for item in items %}
    <input type="hidden" name="_selected_action" value="{{ item.pk }}"/> {# thanks to @David Hopkins for highligting it #}
    {% endfor %}

    {{ form }}
    <p>Apply for:</p>
    <ul>{{ items|unordered_list }}</ul>
    <input type="submit" name="apply" value="Apply" />
 </form>
 {% endblock %}
like image 56
mrvol Avatar answered Nov 07 '22 13:11

mrvol