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 views.py file
contains the following import:
from django.views.generic import (
TemplateView,
View, ListView, DetailView, CreateView, UpdateView, DeleteView)
One of my models is called Organisation inside the application – pbs1/. The
views.py 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',
'currency_Code', 'org_Parent', 'org_Turnover',
'org_Multi_National', 'spec_Name')
model = Organisation
class OrgUpdateView(UpdateView):
fields = ('org_Name', 'org_Site', 'org_HQ_Location', 'org_Sector',
'currency_Code', 'org_Parent', 'org_Turnover',
'org_Multi_National', 'spec_Name')
model=Organisation
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 urls.py 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.
Regards,
Bruckner de Villiers
083 625 1086
From: <[email protected]> on behalf of Valentin Jungbluth
<[email protected]>
Reply to: <[email protected]>
Date: Thursday, 31 October 2019 at 13:45
To: Django users <[email protected]>
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 **views.py** 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 '''
form_class = CommunicationForm
template_name = 'form.html'
success_url = '/HTML_Result/'
def get(self, request):
form = self.form_class()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = self.form_class(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('HTML_Result')
args = {'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_name = 'template_1.html'
def get(self, request):
data = Communication.objects.values().latest('id')
self.donwload_html()
return render(request, self.template_name, {'data': data})
def donwload_html(self):
file_path = os.path.join(settings.MEDIA_ROOT, 'media')
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(),
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/b2cc33dc-1af2-45d0-b8b5-c6b95267beff%40googlegroups.com.
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/4CF0FAC5-4173-4263-8810-9211D7E60E88%40gmail.com.