Thursday, November 24, 2016

Custom admin form not triggering admin Action

Hello,

I've created custom intermediate form for my Admin panel and the
problem is, when
I post that form my action is not triggered (but it is triggered when
I select it to show form).

Do you have any idea what can be wrong? This use case is not well
documented, and only SO question I've found is from 2011 and answer to
it doesn't seem to work ( http://stackoverflow.com/questions/6689741/
)

Here's my admin code:

# admin panel code:

class GroupAdmin(admin.ModelAdmin):
    actions = ['add_subjects_to_group']

    def add_subjects_to_group(self, request, queryset):

        print("GOT REQUEST:", request)
        # it prints only once, and should twice :(

        if 'do_action' in request.POST:
            print("do_action triggered")
            form = SubjectsForm(request.POST)
            if form.is_valid():
                subjects = form.cleaned_data['subjects']
                print("DEBUG:", subjects)
                return
        else:
            form = SubjectsForm()

        return TemplateResponse(request, 'add_subjects_to_group.html',
            {'title': 'Add subjects to group',
             'queryset': queryset,
             'form': form})



And here's my Form template:

{% extends "admin/base_site.html" %}

{% block content %}
    <form action="" method="post">

        {% csrf_token %}
        <input type="hidden" name="action" value="add_subjects_to_group">
        <input type="hidden" name="do_action" value="yes">

        Hello!
        <div>
            {{ form.subjects }}
            <input type="submit" class="default" style="float: none"
value="Submit">
            {{ form.subjects.errors }}
        </div>

    </form>
{% endblock %}

Regards,
Marqin

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d713e440-3fca-422f-a86b-4655895c7420%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment