未加载烧瓶模板

时间:2020-03-26 00:39:11

标签: python html python-3.x flask

晚上好。我试图阻止用户使用reCaptcha v3进入我的网站。但是,尽管被称为:

from flask import url_for, render_template, redirect, request, render_template_string
from flask import current_app as app
from .forms import *
from .filewriter import *
from .mailserver import *
from .rcvalidator import *
from flask_jsglue import JSGlue

jsglue = JSGlue(app)

@app.route('/', methods=('GET', 'POST'))
def home():
    reqform = ReqForm()
    delform = DelForm()
    error = 0
    if request.method == "POST":
        gtoken = request.json['gtoken']
        if not rc_form_valid(gtoken):
            print("working")
            return redirect(url_for('inv')) #would prefer to have the loaded thing here but as a test tried to lad a different function
    return render_template('home.html',
                           reqform=reqform,
                           delform=delform,
                           csrf_token=0,
                           errors=error)


@app.route('/req', methods=('GET', 'POST'))
def req():
    reqform = ReqForm()
    delform = DelForm()
    if reqform.validate_on_submit():
        appendfile("req.csv", reqform)
        mail(reqform.name.data, reqform.tel.data, "recipient")
        return redirect(url_for('success'))
    elif request.method == "POST" and not reqform.validate():
        error = 1 
    else:
        error = 0
    return render_template('home.html',
                           reqform=reqform,
                           delform=delform,
                           csrf_token=reqform.csrf_token,
                           errors = error)


@app.route('/delv', methods=('GET', 'POST'))
def delv():
    reqform = ReqForm()
    delform = DelForm()
    if delform.validate_on_submit():
        appendfile("del.csv", delform)
        mail(delform.name.data, delform.tel.data, "delivery")
        return redirect(url_for('success'))
    elif request.method == "POST" and not delform.validate():
        error = 2
    else:
        error = 0
    return render_template('home.html',
                           reqform=reqform,
                           delform=delform,
                           csrf_token=delform.csrf_token,
                           errors = error)


@app.route('/success')
def success():
    return render_template('success.html') 

@app.route('/invalid', methods=(['GET']))
def inv():
    print("loaded inv") 
    return "get out now"

@app.route('/cancel', methods=('GET', 'POST'))
def cancel():
    canform = CanForm()
    if canform.validate_on_submit():
        # TODO: delete stuff from form
        return redirect(url_for('success'))
    elif request.method == "POST" and not delform.validate():
        error = 1
    else:
        error = 0
    return render_template('cancel.html',
                           canform=canform,
                           csrf_token=canform.csrf_token,
                           errors=error)

打印“正在工作”和“已加载inv”,但是新模板无法渲染,我也不明白为什么。当我提交表单时,success.html呈现。请帮忙

1 个答案:

答案 0 :(得分:0)

一个好的总体做法是知道“ GET”和“ POST”的作用。

在您的代码中,我们知道home()中的“ POST”功能,但不知道“ GET”的功能,因此请尝试以下操作:

@app.route('/', methods=('GET', 'POST'))
def home():
    reqform = ReqForm()
    delform = DelForm()
    error = 0
    if request.method == "POST":
        gtoken = request.json['gtoken']
        if not rc_form_valid(gtoken):
            print("working")
            return redirect(url_for('inv')) 
    elif request.method=='GET':
        return render_template('home.html',
                              reqform=reqform,
                              delform=delform,
                              csrf_token=0,
                              errors=error)