Search code examples
djangopython-3.xdjango-viewsreportlab

Is it possible to forward processed (in views.py) form data for further use?


I am new to django so sorry if I will not succeed to express myself clear enough. To begin with:

I have build simple form to perform simple calculations and return result based on user input. Now I would like to add an option for user to generate PDF from these results, but I am not sure what kind of approach should I use.

My first idea was to store calculation data in some sort of global variable and pass it to the view responsible for generating PDF. According to answer to this question there should be few ways to do that (but I am not sure if they fit because for example my url never changes).

My second idea that since url never changes maybe data is still somewhere and can be accessed somehow? What I am trying to say, that I have calculation results passed in dictionary form (named context in my code) to results html template. Can I still access this dictionary and use it in another views.py function?

Can somebody provide some insights or direct me to what kind of literature I should search (since I do not exactly know what approach I should take and what to look for).

My views.py part responsible for generating form and displaying results:

def PGP_skaiciuokle(request):

    if request.method == 'POST':

        form = PGPskaiciuokle(request.POST)

        pgpData = DuomenuData.objects.get(duomenys = "Lietuvos PGP").data.year
        valData = DuomenuData.objects.get(duomenys = "Valstybiu PGP").data.year

        if form.is_valid():          
            LtPGP = form.cleaned_data['Lietuvos_PGP']
            valstybe = form.cleaned_data['valstybe']
            pValKof = valstybe.pgp            
            iKastai = form.cleaned_data['kastai']

            result = iKastai * (LtPGP/pValKof)
            result = round(result, 2)

            context = {
                'LtPGP': LtPGP,
                'pVal': valstybe,
                'iKastai': iKastai,
                'result': result,
                'pgpData': pgpData,
                'valData': valData,
                }

            return render(request, 'PGPskaiciuokle_valid.html', context)
    else:
        form = PGPskaiciuokle()
        pgpData = DuomenuData.objects.get(duomenys = "Lietuvos PGP").data.year
        valData = DuomenuData.objects.get(duomenys = "Valstybiu PGP").data.year

    context = {
        'form': form,
        'pgpData': pgpData,
        'valData': valData,
    }
    return render(request, 'PGPskaiciuokle.html', context)

Rudiment code for generating pdf in views.py as another function:

import io
from django.http import FileResponse
from reportlab.pdfgen import canvas     

def PGP_result(request):
        buffer = io.BytesIO()
        p = canvas.Canvas(buffer)
    #for now it just hello world, but collected results data should be somehow processed below
    #that a task to figure out after I will know how to collect that data
        p.drawString(100, 100, "hello world.")

        p.showPage()
        p.save()

        buffer.seek(0)
        return FileResponse(buffer, as_attachment=True, filename='hello.pdf')

And relevant part of result displaying html template looks like this:

<div id='sk_body'>
        <fieldset class="rezultatai">
        <legend>Rezultatai</legend>
            <div>Lietuvos PGP: {{ LtPGP }} ({{ pgpData }} m. duomenys)</div>
            <div>Valstybė, kurios kaštai adaptuoti: {{ pVal }} ({{ valData }} m. duomenys)</div>
            <div>Adaptuoti kaštai, vietine valiuta: {{ iKastai }}</div>
            <div class="ats"><b>Rezultas: {{ result }} € </b></div>
        </fieldset>
    </div>
    <div id='sk_btt' style="text-align:center;">
        <form action='PGP_result' method='GET'>
            <button type='submit'>Generuoti PDF</button>
        </form>
    </div>

Please tell me if you need some more info or somewhere I was not clear enough.

I am in no way asking for writing code for me, just asking to point me to right direction-literature, but nevertheless sample code would be very much appreciated.


Solution

  • Almost certainly you want to use the session for this.

    def PGP_skaiciuokle(request):
        ...
            if form.is_valid():
                ...
                request.session['form_data'] = conteext
                ...
    
    def PGP_result(request):
        data = request.session['form_data']