【问题标题】:How can I solve this python problem?(Flask)我该如何解决这个python问题?(Flask)
【发布时间】:2021-02-11 15:46:41
【问题描述】:

代码有什么问题? 请解决? ''' #代码从这里开始

    from flask import Flask, render_template, request
        import mysql.connector 
        from datetime import datetime
        import json
        
        
        
        app = Flask(__name__)
        
        @app.route("/")
        def home():
            return render_template('index.html', params=params)
        
        
        @app.route("/about")
        def about():
            return render_template('about.html', params=params)
        
       #problem appears here 
        @app.route("/contact", methods = ['GET', 'POST'])
def contact():
        f= open("config.json", 'r' ) 
        json.load(f)['params']
        mydb = params ['local_uri']
        mycursor = mydb.cursor()
        
        
        
        if(request.method=='POST'):
                    result = request.form.to_dict()
                    name = result['name']
                    phone_num= int(result['phone'])
                    email = result['email']
                    msg = result['message']
                    mycursor.execute("insert into Contacts(name,phone_num,email,msg) values(%s,%s,%s,%s)", (name,phone_num,email,msg))
                    mydb.commit()
                    mycursor.close()
                    return "success"
                return render_template('contact.html')   
        @app.route("/post")
        def post():
            return render_template('post.html',params=params)
        
        
        app.run(debug=True)'''
'''json.decoder.JSONDecodeError json.decoder.JSONDecodeError:无效的控制字符:第 5 行第 44 列 (char 92)'''

【问题讨论】:

  • 您的问题是什么?正在发生什么,与您预期发生的情况相比如何?包括您收到的任何错误和回溯
  • 您提到“问题出现在这里”,在该路由中没有参数声明,您正在尝试访问它。
  • 您遇到了什么错误。通过输出和错误更正您的问题,以便社区可以提供帮助。

标签: python flask mysql-connector-python


【解决方案1】:

很难从这里猜出你在说什么,但你没有包含“def contact():”行,这意味着你错过了一些语法。尝试用下面的代码替换那部分代码:

@app.route("/contact", methods = ['GET', 'POST'])
def contact():
    f= open("config.json", 'r' ) 
    json.load(f)['params']
    mydb = params ['local_uri']
    mycursor = mydb.cursor()

【讨论】:

    猜你喜欢
    • 2023-02-19
    • 2020-03-25
    • 2020-02-14
    • 2019-02-08
    • 1970-01-01
    • 2021-10-23
    相关资源
    最近更新 更多