1

我的表单通过 sijax 请求提交数据,并使用 obj_response 确认提交。我尝试了 obj_response.alert() 和 obj_response.html() 但没有任何效果

我的代码:

from flask import Flask, render_template, request, flash, g, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
import flask_sijax
import os
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///Login.sqlite3"
path = os.path.join('.', os.path.dirname(__file__), 'static/js/sijax1/')
app.secret_key = 'super secret key'
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SIJAX_STATIC_PATH'] = path
app.config['SIJAX_JSON_URI'] = 'static/js/sijax/json2.js'

db = SQLAlchemy(app)
flask_sijax.Sijax(app)


class Signup(db.Model):
    ID = db.Column("ID", db.String(10), primary_key=True)
    Fname = db.Column(db.String(20))
    LName = db.Column(db.String(20))
    Password = db.Column(db.String(20))


    def __init__(self, id, fname, lname, password):
        self.ID = id
        self.Fname = fname
        self.LName = lname
        self.Password = password


@flask_sijax.route(app, '/signup')
def home():
    def caller(obj_response, values):
        newuser = Signup(values['ID'], values['fname'], values['lname'], values['Password'])
        db.session.add(newuser)
        db.session.commit()
        obj_response.html("#result", "Successfully signed up")


    if g.sijax.is_sijax_request:
        g.sijax.register_callback('call', caller)
        return g.sijax.process_request()
    return render_template("first.html")


@app.route("/success", methods=['POST', 'GET'])
def success():

    records = db.session.query(Signup).all()
    return render_template("second.html", records=records)

if __name__ == "__main__":
    db.create_all()
    app.run(host="127.0.0.1", port=2908, debug=True, threaded=True)

我的 HTML 文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sign Up</title>
    <script type="text/javascript" src="{{url_for('static',filename='Signup.js')}}"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript" src="{{url_for('static', filename='js/sijax/sijax.js')}}"></script>
    <script type="text/javascript">{ {g.sijax.get_js()|safe }}</script>

    <link rel="stylesheet" href="{{url_for('static', filename='styles/firstcss.css')}}">
</head>
<body style = "font-family: Lato">
    <div style="padding-left: 5%;padding-top:0.2%;padding-bottom: 0px;height: 4%;width: 100%;background-color: #11557C;margin-left: 0px;margin-top: 0px;">
    <h2>Welcome to my site</h2><br>
    </div>
    <div style="margin-left: 70%" width="100%">
        <form method="post" action="http://127.0.0.1:2908/success">
        <input type="submit" style="position: relative;margin-top: 5%;margin-left:20%" value="View Logs">
        </form>
    </div>

    <div style="width:80%">
    <form  name="form1" id="form1" method="post" style="padding-top: 1%"><!--action = "http://127.0.0.1:2908/success"!-->
        <label id="head" style="font-size: 20px;padding-bottom: 1%;">Sign Up</label><br>
        <label id="user" style="font-size: 16px;margin-top: 5%;">Username</label><br>
        <input type="text" name = "ID" placeholder = "Username" style = "padding-left: 0.2%;"><br><br>
        <label style="font-size: 16px">First name</label><br>
        <input type="text" name = "fname" id="fname" placeholder = "First name" style = "padding-left: 0.2%"><br><br>
        <label style="font-size: 16px">Last name</label><br>
        <input type="text" name = "lname" placeholder = "Last name" style = "padding-left: 0.2%"><br><br>
        <label style="font-size: 16px">Password</label><br>
        <input type = "password" name = "Password" placeholder = "Password" style = "padding-left: 0.2%"><br><br><br>
        <input type = "submit" onclick="return validate()" value = "Sign up" style="width:50%;height: 40px;margin-left: 12%;background-color: #11557C;color: black">
    </form>
    </div>
    <h3 id="result" >RESULT</h3>


</body>
</html>
4

1 回答 1

0

我注意到 Sijax 在使用高于 1.5.1 的 jQuery 版本时停止工作所以尝试替换:

https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js

和:

https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js

但是在这一点上,我没有从其他开发人员或论坛中找到任何其他确认,只是我自己的。

于 2017-06-25T14:54:46.377 回答