Is it possible to access values submitted through HTML Form, rendered in Preview page, in another views.py method

Viewed 34

I am newbie in a django framework. And trying to print PDF from the submitted data through HTML form. For that I have 2 stages.

  1. Fillup the form
  2. Preview(result) those filled up data
  3. Generate PDF from the preview(result) page

I have managed to post data using HTML form.

My views.py

def preview(request):
    if request.method == "POST":
        formvals = Formdata()
        formvals.date = request.POST["date"]
        formvals.name = request.POST["name"]
    
        return render(request, "result.html", {'formvals':formvals})

def getpdfPage(request):
    formvals = preview(request)
    print(formvals.date)
    print(formvals.name)
    data = {'date':formvals.date, 'formvals.name'}
    template = get_template("pdf_page.html")
    data_print = template.render(data)
    response = BytesIO()
    pdfPage = pisa.pisaDocument(BytesIO(data_print.encode("UTF-8")),response)
    if not pdfPage.err:
        return HttpResponse(response.getvalue(),content_type="application/pdf")
    else:
        return HttpResponse("Error faced")

Urls.py

urlpatterns = [
    path('',views.home, name='home'),
    path('getpdfPage',views.getpdfPage, name='getpdfpage'),
    path('preview',views.preview, name='preview')
]

Here I get None values when i try to print data dict in getpdfPage()

Is there any way to access the data of preview() in getpdfPage().

Thanks in advance

2 Answers

You should not access formvals.date, formvals.name like this, but you should replace

        formvals = Formdata()
        formvals.date = request.POST["date"]
        formvals.name = request.POST["name"]

with

        formvals = Formdata()
        formvals.update({'date': request.POST["date"], 'name': request.POST["name"]}

What I would do is save the preview formdata into a model.

Once you have a model with the saved data that represents this form then you can access this model instance from any view by just doing a query, for example: YourModel.objects.filter(date=some_date) or YourModel.objects.filter(name=some_name)

Related