1
How do I get a PDF in the Django admin? I already have the views, I can generate in the frontend with HTML
all right, but how do I put a button in the admin to generate or redirect to the pdf link? I’m lost in this.
class EmpresaAdmin(admin.ModelAdmin):
list_display = ('nome', 'telefone', 'cnpj',)
search_fields = ('nome', 'cnpj',)
list_filter = ( 'nome', 'cnpj', 'telefone', )
class AssociadoAdmin(admin.ModelAdmin):
list_display = ('nome', 'endereco', 'telefone', 'cpf', 'data_filiacao', 'data_nascimento' )
search_fields = ('nome', 'cpf',)
list_filter = ( 'nome', 'cpf', 'telefone', )
class AgendamentoAdmin(admin.ModelAdmin):
list_display = ('nome','inicio', 'fim','assunto', )
search_fields = ('nome', 'cpf',)
list_filter = ( 'nome', 'inicio', 'assunto', )
views
class Render:
@staticmethod
def render(path: str, params: dict, filename: str):
template = get_template(path)
html = template.render(params)
response = io.BytesIO()
pdf = pisa.pisaDocument(
io.BytesIO(html.encode("UTF-8")), response)
if not pdf.err:
response = HttpResponse(
response.getvalue(), content_type='application/pdf')
response['Content-Disposition'] = 'attachment;filename=%s.pdf' % filename
return response
else:
return HttpResponse("Error Rendering PDF", status=400)
Generating a button in the admin is not difficult. Choose one of the models and create a function that returns the html of the button. In admin.py list the button as if it were a field but include it in the read_only_fields list.
– André Duarte
What would this function be like, friend? I’m new to Django, I managed to create with a template on the front, but in admin.
– Acelino Fernandes