Issue
as the title says: how can I (in DJANGO) get data from a form in a view (in the code below is the ALIMENTA2 view) and then use that as context in another class-based view (GMO, which is a PDF report built with easy_pdf)?
I'm a noob at django, but I've tried redirect and render... I don't seem to understand exactly what I'm doing, really hahaha
views.py
def alimenta2(request):
if request.method == 'POST':
form = AlimentaForm(request.POST)
if form.is_valid():
day = form.cleaned_data['sign_date']
shipper = form.cleaned_data['shipper']
context = {'day': day, 'shipper': shipper}
#HERE IS THE PROBLEM, I WANT TO PASS THE CONTEXT:
return redirect('GMO', context=context)
else: form = AlimentaForm()
return render(request, 'agroex/alimenta2.html', {'form':form})
class GMO(PDFTemplateView):
template_name = 'agroex/gmo.html'
def get_context_data(self, **kwargs):
context = super(GMO, self).get_context_data(
pagesize='A4',
title='NON-GMO Certificate',
day=self.day,
**kwargs
)
urls.py
urlpatterns = [
path('', views.agroex, name='agroex'),
path('alimenta2/', views.alimenta2, name='alimenta2'),
path('alimenta2/GMO/', views.GMO.as_view(), name='GMO'),
]
Solution
You can store the variables in session and then retrieve in the other view, like this:
def alimenta2(request):
if request.method == 'POST':
form = AlimentaForm(request.POST)
if form.is_valid():
day = form.cleaned_data['sign_date']
shipper = form.cleaned_data['shipper']
request.session['day'] = day
request.session['shipper_id'] = shipper.id
return redirect('GMO')
else:
form = AlimentaForm()
return render(request, 'agroex/alimenta2.html', {'form':form})
class GMO(PDFTemplateView):
template_name = 'agroex/gmo.html'
def get_context_data(self, **kwargs):
day = request.session['day']
shipper_id = self.request.session['shipper_id']
shipper = Shipper.objects.get(id=shipper_id)
context = super(GMO, self).get_context_data(
pagesize='A4',
title='NON-GMO Certificate',
day=day,
shipper=shipper,
**kwargs
)
Answered By - alfredo138923
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.