I am using Django Admin action but I want to use it in modal form

I am new to django and I am using Django admin actions which gives us action admin action dropdown. I am using django-admin-actions 0.1.1

I created view

# views.py
from django.http import HttpResponse
from django.template.loader import render_to_string
from weasyprint import HTML, CSS

from .models import StudentEnrollment

# Django actions
def generate_enrollment_form_pdf(request):
    # Query Student Enrollment data
    enrollments = StudentEnrollment.objects.all()

    # Render HTML template with data
    html_string = render_to_string('enrollment_form.html', {'enrollments': enrollments})

    # Create PDF from HTML with A5 size
    pdf_file = HTML(string=html_string).write_pdf(stylesheets=[CSS(string='@page { size: A6; }')])

    # Return PDF response
    response = HttpResponse(pdf_file, content_type='application/pdf')
    response['Content-Disposition'] = 'filename="enrollment_form.pdf"'

    return response

# Django actions over

and setup urls.py

# urls.py

from django.urls import path

from .views import *

urlpatterns = [
    path('generate-enrollment-pdf/', generate_enrollment_form_pdf, name='generate_enrollment_pdf'),

]

In admin.py I create a function for my plan execution xD

#  Function for Prep Enrollment Print
def print_enrollment_form_pdf(modeladmin, request, queryset):
    # Query Student Enrollment data
    enrollments = queryset

    # Render HTML template with data
    html_string = render_to_string('enrollment_form.html', {'enrollments': enrollments})

    # Create PDF from HTML
    pdf_file = HTML(string=html_string).write_pdf()

    # Return PDF response
    response = HttpResponse(pdf_file, content_type='application/pdf')
    response['Content-Disposition'] = 'filename="enrollment_form.pdf"'

    return response


print_enrollment_form_pdf.short_description = "Print Enrollment Form PDF"

and then i call that action in my modalAdmin

@admin.register(StudentEnrollment)
class StudentEnrollmentAdmin(ImportExportModelAdmin):
    resource_class = StudentEnrollmentResource
    list_display = ('id', 'get_student_name', 'session', 'class_name', 'calculated_age')
    list_display_links = ('id', 'get_student_name', 'session', 'class_name', 'calculated_age')
    list_filter = ('session', 'class_name')
    search_fields = ('student__student_name__first_name', 'student__student_name__last_name')
    autocomplete_fields = ["student", 'session', ]
    readonly_fields = ['class_name', ]

    # action call
    actions = [print_enrollment_form_pdf]

it is so far so Good I get action in action bar enter image description here

But I want that same action in my modal from, I am using modal form to add custom buttons enter image description here

so I added button via change_form.html and also added url in

{% extends 'admin/change_form.html' %}

{% block submit_buttons_bottom %}
  {{ block.super }}

    <form id="enrollmentForm" action="{% url 'generate_enrollment_pdf' %}" method="POST" enctype="multipart/form-data" class="form-control">
     {% csrf_token %}

  <!-- Button for Save and Print -->
  <input type="button" value="Save and Print"/>
</form>



{% endblock %}

But when i click on save and print button nothing would happen I want it to save model data and print as my django admin action does.

like this (https://i.stack.imgur.com/ckwY0.png)

A couple of thoughts:

  • The django-admin-actions package is 5 years old. That means it hasn’t been officially tested or even upgraded for anything newer than 2.2 It’s certainly not a project I would rely upon.

  • You’re submitting your form to {% url 'generate_enrollment_pdf' %}, which is your generate_enrollment_form_pdf view. This view doesn’t do anything with the form data being submitted. There’s no code in there to handle POST data.

  • You wrote:

But you don’t provide any details about what does happen. What is the result on the page? Do you see the POST request being made on your server console? What are the sequence of requests you see related to this? Are there any error messages generated anywhere? (Either in the browser or on the server)

I want want to attach my print action in print and save button.
and if django admin is old what would you suggest me to use?

For clarity - it’s the third-party package “django-admin-actions” that is out-of-date. (The Django admin itself is well-maintained.)

I don’t really know what it does, so I don’t have any alternative suggestions for a replacement.

Aside from that, the other two bullet points in my previous post address other issues.

I want to Print some data as PDF and fetch details from my Modals but I cannot append django admin actions in Custom button in modal form.

I am not sure whether this is a solution to OP’s problem, but recently I created a package for adding intermediate pages for actions which allows passing custom values to them.

https://pypi.org/project/django-admin-action-forms

It uses build in django forms and reuses templates from change form, it also supports filter_horizontal and autocomplete_fields. It should support most use cases, but I am open to suggestions.

I plan on maintaining it in the future as it is something I will uses in my projects.
I hope you find it useful. :slight_smile:

Thanks, I will give it a try for my next projects.