如何在href标签中使用flask变量?

时间:2018-03-09 12:11:46

标签: python flask

下面是使用flask和python上传文件的代码,然后我想在href标签中使用filename变量!

我正在尝试{{filename}},但我无法访问它。

    def index():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            path = "./audiofile.txt"
            filepath = open(path, "w")
            filepath.write(filename)           
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            filepath.close()
            os.system('python ../speech-to-text/slow.py')
            os.system('python ../sentiment-analysis/test.py')
            return redirect(url_for('index'))
    return """
    <!doctype html>
    <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Online audio/video transcription!</title>

  <link rel="stylesheet" type="text/css" href="static/bootstrap.min.css"/>
  </head>
    <title>Upload new File</title><br>
    <center>
    <h1>Upload new File</h1><br>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file><br/><br/>
         <input class=btn-success type=submit value=Upload>
    </form>
    <hr>
    <h4>Download transcript file (with sentiment analysis) <a href="tmp/{{filename}}.txt">here</a>!</h4>
    </center>

    <p>%s</p>
    """ % "<br>"

1 个答案:

答案 0 :(得分:1)

问题有点模糊,但我会尝试回答我猜你在问的问题。

为了在使用Jinja2 synthax的HTML中使用filename变量,首先必须将该文件名传递给HTML。

因此您的简化视图功能将如下所示

import SpriteKit
class GameScene: SKScene {
    var sprite: SKSpriteNode!
    var touchPoint: CGPoint = CGPoint()
    var touching: Bool = false
    override func didMove(to view: SKView) {
        self.physicsBody = SKPhysicsBody.init(edgeLoopFrom: self.frame)
        sprite = SKSpriteNode(color: .red, size: CGSize(width: 50, height: 50))
        sprite.physicsBody = SKPhysicsBody.init(rectangleOf: sprite.size)
        sprite.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)
        self.addChild(sprite)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first?.location(in: self) {
            if sprite.frame.contains(touch) {
                touchPoint = touch
                touching = true
            }
        }
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches {
            let location = t.location(in: self)
            touchPoint = location
        }
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        touching = false
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        touching = false
    }

    override func update(_ currentTime: TimeInterval) {
        if touching {
            let dt:CGFloat = 1.0/60.0
            let distance = CGVector(dx: touchPoint.x-sprite.position.x, dy: touchPoint.y-sprite.position.y)
            let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
            sprite.physicsBody!.velocity = velocity
        }
    }
}

将file_name传递给HTML后,您就可以开始使用Jinja2 synthax {{file_name}}