【问题标题】:python variable is not getting displayed on HTML file while using ```{{ }}``` (jinja2) and flask使用```{{}}```(jinja2)和flask时python变量没有显示在HTML文件上
【发布时间】:2021-11-13 09:21:25
【问题描述】:

我想做一个信用卡生成器,生成格式为 (number|exp.month|exp.year|cvv) 的信用卡,信用卡号遵循 luhn 算法。用户输入一个 BIN(银行识别号),我使用 python 生成一个十六位数字的信用卡。我将所有信用卡存储在python变量output中,但是当我尝试使用jinja2函数{{ output }}在html页面上显示它时,它不会在html页面上显示。

我很确定 Python 代码的所有 html 代码以及数学和循环部分都是正确的。

当我尝试使用时

def function1:
   return (output)

它没有显示错误,并显示了所需的输出。但我希望它在 HTML 文件本身中,而不是在新的单独页面上加载。

索引.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 存储库链接:https://github.com/FieryKing01/Python-CC-generator

【问题讨论】:

  • 我永远离开了jinga。现在我什至不知道如何拼写它。 Jinga是浪费时间。我应该只是学习 JS。

标签: python flask jinja2


【解决方案1】:

在我看来,这里的问题是您必须将output 变量“提供”给render_template,因此,例如:

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

【讨论】:

  • 我已经对代码进行了必要的更改,但仍然出现相同的错误。 @jmaloney13
  • @FieryKing - 看起来,在编辑过程中,您将 'return render_template('index.html')' 移到了函数顶部的 'if' 语句之前。我认为这可能是问题所在。
  • 我把它移到了原来的位置。但还是同样的问题@jmaloney13
  • 我会尝试将它移动到之前的位置,看看是否可行。但是,老实说,我不明白你在这里所做的一切。但是,现在在哪里, function1() 只会返回 render_template('index.html') 而没有“输出”。它永远不会到达“if”语句中的任何内容,因为它会在到达第一个“return”后停止。
  • 我不明白。如果可能,请您进行必要的更改并通过 fieryking02@protonmail.com 与我分享,以便我更好地理解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
  • 1970-01-01
  • 2021-11-27
  • 1970-01-01
相关资源
最近更新 更多