0

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'),
]

2 Answers2

0

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
       )
alfredo138923
  • 1,509
  • 1
  • 15
  • 15
  • Hello Alfredo. Thanks for your response. I didn't know about sessions, and after reading about them on the docs and other sources it made much more sense. Also, I wasn't able to work out the request.session part on the TemplateView class because we need to add a self. before it (i.e. day=self.request.session). However, I don't seem to be able to pass "dates" via this session procedure. Is there any workaround? – Victor Dias Mar 30 '19 at 05:52
  • You can convert the date to string `str(day)`. Check some alternatives here [link](https://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable) – alfredo138923 Mar 30 '19 at 22:46
  • I don't think request will work like that in get_context_data, you need to use self.request – Conor Aug 22 '22 at 13:00
0

You can retrieve the data on a form field like this if using a class-based view:

class RecordUpdatePage(UpdateView):
    
    model = DashboardModel
    template_name = "dashboard/record_update.html"
    form_class = RecordForm
    success_url = [your success url]
  
    
        def get_context_data(self, **kwargs):
    
            form_class = self.get_form_class()
            form = self.get_form(form_class)
    
            var = form['my_form_field_name']
    
            print(var.value())
    
            # return a new context if you want
            context['modified_field'] = int(var.value()) + 1000
    
            return context

As this is a CBV example, you would define this class as a view in urls.py:

  path('dashboard/record_update/<str:pk>/', RecordUpdatePage.as_view(),  name= 'record_update'),
Conor
  • 327
  • 6
  • 15