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
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
--
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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b2cc33dc-1af2-45d0-b8b5-c6b95267beff%40googlegroups.com.