Fiery King
Fiery King

Reputation: 9

python variable is not getting displayed on HTML file while using ```{{ }}``` (jinja2) and flask

I want to make a credit card generator that generates credit cards in the format (number|exp.month|exp.year|cvv) and the credit card number follows the luhn algorithm. The user inputs a BIN(Bank Identification Number) starting from which, I generate a sixteen digit credit card using python. I store all the credit cards in a python variable output but when I try to display it on the html page using jinja2 function {{ output }}, it does not show it on the html page.

I am pretty sure that all the html code and math and loop part of python code is correct.

When I try using

def function1:
   return (output)

It shows me no error and gives shows the desired output. But I want this in the HTML file itself and not to load on a new separate page.

Index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>
            Home
        </title>
    </head>
    <body>
        <h1>
            Fire CC Generator
        </h1>

        <form action="{{ url_for("function1") }}" method="post">
            <label for="Bin">B.I.N.</label>
            <input type="text" name="Bin" placeholder="Enter BIN"><br>
            <input type="text" name="quantity" placeholder="Enter Quantity"><br>
            <button type="button" value="Submit">
        </form>
        <p>
            Output: {{output}}
        </p>
    </body>
</html>

index.py:

import random
from flask import Flask, render_template, request, url_for
app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def function1():
   if request.method == 'POST':

      # getting input with name = Bin in HTML form

      Bin = request.form.get('Bin')

      # getting input with name = lname in HTML form

      quantity = int(request.form.get('quantity'))
      x = 0
      ccnum = 0
      good = 16 - len(Bin)

      output = ''
      exp_month_list = [
         '01',
         '02',
         '03',
         '04',
         '05',
         '06',
         '07',
         '08',
         '09',
         '10',
         '11',
         '12',
         ]

      while x != quantity:
         r = str(random.randint(pow(10, good - 1), pow(10, good)- 1))
         iccnum = Bin + r

         # Luhn Algorithm Begins

         def luhn_checksum(card_number):

            def digits_of(n):
               return [int(d) for d in str(n)]
            digits = digits_of(card_number)
            odd_digits = digits[-1::-2]
            even_digits = digits[-2::-2]
            checksum = 0
            checksum += sum(odd_digits)
            for d in even_digits:
               checksum += sum(digits_of(d * 2))
            return checksum % 10

         # Luhn Algorithm Ends

         if luhn_checksum(int(iccnum)) == 0:
            ccnum = iccnum
            ccnum = str(ccnum)
            exp_month = random.choice(exp_month_list)
            exp_year = str(random.randint(2022, 2026))
            cvv = str(random.randint(100, 999))
            CC = str(ccnum + '|' + exp_month + '|' + exp_year + '|'+ cvv + '\n')
            output += CC
            x += 1
         else:
            x = x
         return render_template('index.html', output=output)
   return render_template('index.html')
if __name__ == '__main__':
    app.run()

GitHub Repository link: https://github.com/FieryKing01/Python-CC-generator

Upvotes: 0

Views: 171

Answers (1)

jmaloney13
jmaloney13

Reputation: 235

It looks to me like the issue here is that you've got to 'feed' the output variable to render_template, so, like:

return render_template('index.html', output = output)

Upvotes: 1

Related Questions