Thursday, October 31, 2019

Re: Export HTML template filled by form to HTML file

If I haven't misunderstood, you wish to add/update/list data to/from the DB via a form?  I am learning myself and I have just successfully  managed to achieve this through CBV's (Class Based Views), which obviate the use of forms and perform well – with fewer lines of code. In Django 2.2.x the file contains the following import:

from django.views.generic import (


    View, ListView, DetailView, CreateView, UpdateView, DeleteView)


One of my models is called Organisation inside the application – pbs1/.  The file contains the following classes:

class Org_View(ListView):

    model = Organisation

    # Returns organisation_list


class Org_DetailView(DetailView):

    context_object_name = "org_detail"

    model = Organisation

    template_name = 'pbs1/organisation_detail.html'

    # Returns organisation


class OrgCreateView(CreateView):

    fields = ('org_Name''org_Site''org_HQ_Location''org_Sector',


    model = Organisation


class OrgUpdateView(UpdateView):

    fields = ('org_Name''org_Site''org_HQ_Location''org_Sector',




class OrgDeleteView(DeleteView):

    model = Organisation

    success_url = reverse_lazy("pbs1:organisation_list")


Each class does exactly what it says – e.g. OrgUpdateView allows me to update a previously created record in the DB.  The and *.html are a bit more complicated.  But, I can share these with you as well.  I learnt/am learning this stuff through Python and Django Full Stack Web Developer Bootcamp from Udemy.


Bruckner de Villiers

083 625 1086


From: <> on behalf of Valentin Jungbluth <>
Reply to: <>
Date: Thursday, 31 October 2019 at 13:45
To: Django users <>
Subject: Export HTML template filled by form to HTML file


I have a template page on which one I access when I filled my `Django form`. This page is a simple `HTML page` with data coming from my form. I would like to be able to download the filled template. That's to say, get a browser window which let to download the template or by clicking on a button which will save the HTML template somewhere.


This is my code in my **** file:



 class TemplateGenerator(TemplateView):
''' This class displays the form and lets to save data into my database.
            It redirects users to the filled HTML template '''

= CommunicationForm
= 'form.html'
= '/HTML_Result/'
def get(self, request):
= self.form_class()
return render(request, self.template_name, {'form': form})
def post(self, request):
= self.form_class(request.POST, request.FILES)
if form.is_valid():
return redirect('HTML_Result')
= {'form': form}
return render(request, self.template_name, args)
class HTMLResult(TemplateView):
''' This class displays the template filled thanks to the form from TemplateGenerator() class '''
= 'template_1.html'
def get(self, request):
= Communication.objects.values().latest('id')
return render(request, self.template_name, {'data': data})
def donwload_html(self):
= os.path.join(settings.MEDIA_ROOT, 'media')
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
= HttpResponse(,
['Content-Disposition'] = 'inline; filename=' + 'test'
return response
raise Http404



I know I need to use `Content-Disposition` in order to download the HTML page, but I don't find a way to use it correctly.
Do you have any idea ?


Thank you by advance

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
To view this discussion on the web visit

No comments:

Post a Comment