Home > other >  Django Add 2 Numbers and get Name and print it back in an html
Django Add 2 Numbers and get Name and print it back in an html

Time:11-11

I'm new to python and django What is the correct syntax to render number and text? I want to get the name and add 2 numbers from addition.html and print it back to results.html

I tried this code

def add(request):
    my_context = {
     "fname" : (request.GET['first_name']),
        val1 = int(request.GET['num1']),
        val2 : int(request.GET['num2']),
         res : val1   val2
    #return render(request, "result.html", {'result': res})
      }
    return render(request, "result.html",my_context)

CodePudding user response:

Both

 return render(request, "result.html", my_context)
 return render(request, "result.html", {'result': res})

will work. render syntax:

Next you need to use the context variables in results.html

results.html // if you used the first one otherwise only 'result' can be used in the html file

 {{ fname }}
val1 is  {{ val1 }}
val2 is  {{ val2 }}
result is {{ res }}

You can also use if ..else, iterate, filter, ... in template refer

  • Related