1

I'm asking a question about variables handling in my Django application view.

I have 2 functions :

  • The first one lets me to display query result in an array with GET filter parameter (in my case, user writes year and Django returns all objects according to this year. We will call query_naissance this variable).

  • The second one lets me to create a PDF. I have lots of variables but I want to take one more time query_naissance in my PDF.

This is my first function :

@login_required
def Table_annuelle_BirthCertificate(request) :

    query_naissance = request.GET.get('q1')
    ...
    return render(request, 'annuel.html', context)

And my second function looks like :

@login_required
def Table_Naissance_PDF(request) :

    data = {"BirthCertificate" : BirthCertificate}

    template = get_template('Table_raw.html')
    html  = template.render(Context(data))

    filename = str('Table annuelle Naissance.pdf')
    path = '/Users/valentinjungbluth/Desktop/Django/Individus/' + filename


    file = open(path, "w+b") 
    pisaStatus = pisa.CreatePDF(html.encode('utf-8'), dest=file, encoding='utf-8')
    file.close()

    context = {
        "BirthCertificate":BirthCertificate,
        "query_naissance":query_naissance,
    }

    return render(request, 'Table.html', context) # Template page générée après PDF

So How I can add query_naissance given by user in my first function to my second one without write one more time a field ?

Then, I have to call this variable like {{ query_naissance }} in my HTML template.

Thank you

2
  • Ok I am making some researches about value storing in the session. Commented Feb 8, 2017 at 11:10
  • Thank you very much ! I just have to see why my variable doesn't display on my PDF but your script seems to work ;) Please add an answer in order to valide it ;) Commented Feb 8, 2017 at 11:19

1 Answer 1

6

In order to persist information across requests, you would use sessions. Django has very good session support:

# view1: store value
request.session['query_naissance'] = query_naissance

# view2: retrieve vlaue
query_naissance = request.session['query_naissance'] 
# or more robust
query_naissance = request.session.get('query_naissance', None)

You need 'django.contrib.sessions.middleware.SessionMiddleware' in your MIDDLEWARE_CLASSES.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.