Home > database >  name 'c' is not defined in python
name 'c' is not defined in python

Time:06-14

I tried to make a calculator with Django, but got this error

This view function takes the input from html page and performs the calculation.

Where is the problem here?

views.py

from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
# Create your views here.

def indx(request):
    template = loader.get_template('calculator/calc_home.html')

    if request.method=='POST':
        global c, a, b
        a = request.POST.get('num1')
        b = request.POST.get('num2')

        option = request.POST.get('select_val')

        if a == "" or b == "" :
            c = 'please select 2 numbers'
        else:   
            if option == 'add':
                c = int(a)   int(b)
            elif option == 'multiply':
                c = int(a) * int(b) 
            elif option == 'substract':
                c = int(a) - int(b)
            elif option == 'devide':
                if int(b)   0 == 0:
                    c = 'infinity'
                else:   
                    c = int(a) / int(b)         
            else:
                c = 'please choose an option'   

    context = {
        'sum': c,
    }

    return HttpResponse(template.render(context, request))

calc_home.html

html code takes the input and passes it to the view

<h1>welcome to calculator</h1>

<p>enter 2 numbers</p>

<form method="POST">
    {% csrf_token %}
    <label for="num_1">enter 1st number:</label>
    <input type="number" name="num1" value='num_1'><br>
    
    <label for="num_2">enter 2nd number:</label>
    <input type="number" name="num2" value='num_2' ><br>
    
    <input type="radio" value="add" name="select_val" id="add" >
    <label for ="add"> </label><br>
    <input type="radio" value="substract" name="select_val" id="subs" >
    <label for ="subs">-</label><br>
    <input type="radio" value="multiply" name="select_val" id="multiply" >
    <label for ="multiply">*</label><br>
    <input type="radio" value="devide" name="select_val" id="devide" >
    <label for ="devide">/</label><br>

    <input type="submit" name="submit"  value="submit">
</form>
    
<h1>{{ sum }}</h1>

<br><a href="{% url 'home:homex' %}">go to home</a>

Error

This error is coming in the html page

name 'c' is not defined

Request Method: GET
Request URL:    http://127.0.0.1:8000/calculator/
Django Version: 4.0.5
Exception Type: NameError
Exception Value:    
name 'c' is not defined

what should be done to get rid of this error?

CodePudding user response:

  1. You don't need globals here (why do you think you do?)
  2. Your problem stems from c not necessarily always having a value due to how your ifs are nested. (c doesn't get assigned any value if the request method isn't POST, and you still try to use it in the template context.)
  3. You should never need to use loader.get_template() in regular use.

You'll have a better time if you refactor your code to use a plain old function to do the calculation, and call that in your view:

from django.shortcuts import render


def do_calculation(a, b, option):
    if not (a and b):
        return 'please select 2 numbers'
    if option == 'add':
        return int(a)   int(b)
    if option == 'multiply':
        return int(a) * int(b)
    if option == 'substract':
        return int(a) - int(b)
    if option == 'devide':
        if int(b)   0 == 0:
            return 'infinity'
        return int(a) / int(b)
    return 'please choose an option'


def indx(request):
    result = None
    if request.method == 'POST':
        a = request.POST.get('num1')
        b = request.POST.get('num2')
        option = request.POST.get('select_val')
        result = do_calculation(a, b, option)

    return render(request, 'calculator/calc_home.html', {
        'sum': result,
    })

Beyond these improvements, you should look into Django's forms to further DRY out your code.

  • Related