1

I want to add context in my create_form view but I get

UnboundLocalError: local variable 'range' referenced before assignment

The context of create_form view should available in create_test_bills.

from django.shortcuts import render, redirect
from .forms import TestForm
from .models import Test
from django.template.loader import get_template
from django.http import HttpResponse

from project.utils import render_to_pdf 

def create_form(request):
    if request.method == 'POST':
        range = request.POST.get('range')
        results = request.POST.get('results')
        unit = request.POST.get('unit')
        print(range,results,unit)
        return redirect('test_bills')

    return render(request,'form.html',{})    


def create_test_bills(request,*args, **kwargs):

    if request.method == 'POST':
        range = request.GET.get('range')
        results = request.GET.get('results')
        unit = request.GET.get('unit')
    pdf = render_to_pdf('invoice.html',{'range':range,'results':results,'unit':unit,})

    return HttpResponse(pdf, content_type='application/pdf')

My template for form.html . I have included some fields required .

<form method="POST">
    {% csrf_token %}
    <div class="container">
      <input type="text" placeholder="Range" name="range" required>
      <input type="text" placeholder="Results" name="results" required>
      <input type="text" placeholder="Unit" name="unit" required>
      <button type="submit" class="registerbtn">Register</button>
    </div>
  </form>
</body>
</html>
Chris
  • 2,162
  • 1
  • 6
  • 17
manish
  • 302
  • 2
  • 15
  • 1
    Always post **the whole error message** with **full traceback** please. Also fix your indentation. – Selcuk Feb 05 '20 at 03:07

1 Answers1

3

I see a number of issues in your code. First of all you need to be aware that a redirect is (at least to my knowledge) a GET request. So you are sending your data to the first view using a POST request and passing it on with a GET request.

Secondly, in your create_test_bills view you are checking for the request method to be a POST and then you are trying to fetch the GET parameters. So you will never get your parameters here.

Thirdly, in your create_test_bills view, you are trying to render a pdf regardless of whether the request is a POST or not. As a redirect is a GET request your code below if request.method == "POST" is not called and you are getting the error you are seeing.

In order to pass the parameters I see two approaches. One way is to construct the redirect url in your create_form view, e.g.:

def create_form(request):
    if request.method == 'POST':
        range = request.POST.get('range')
        results = request.POST.get('results')
        unit = request.POST.get('unit')
        print(range,results,unit)
        url = reverse('test_bills')+'?range=%s&results=%s&unit=%s' % (range, results, unit)
        return redirect(url)

    return render(request,'form.html',{})

The second approach is using sessions to store your POST data as described in this SO post, e.g.

def create_form(request):
    if request.method == 'POST':
        request.session['_my_data'] = request.POST
        return redirect('test_bills')

    return render(request,'form.html',{})

def create_test_bills(request):

    data = request.session.get('_my_data')
    pdf = render_to_pdf('invoice.html',{'range':data['range'],'results':data['results'],'unit':data['unit'],})

    return HttpResponse(pdf, content_type='application/pdf')
Chris
  • 2,162
  • 1
  • 6
  • 17