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)