简体   繁体   English

App.route 在 Flask 中不适用于一种方法

[英]App.route doesn't work in Flask for one method

Here's my code:这是我的代码:

from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime

app = Flask(__name__)
app._static_folder = 'static/'
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:test@localhost/Scheduling'
db = SQLAlchemy(app)


class CPR_INFO(db.Model):
   DEAL_ID = db.Column(db.String(50), primary_key=True)
   POG_NUM = db.Column(db.String(50))
   OPOG_START_DT = db.Column(db.Date())
   OPOG_END_DT = db.Column(db.Date())

def __repr__(self):
    return 'Deal_ID ' + str(self.DEAL_ID)


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


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

if request.method == 'POST':
    post_id = request.form['DEAL_ID']
    post_title = request.form['POG_NUM']
    post_content = request.form['OPOG_START_DT']
    new_post = CPR_INFO(DEAL_ID = post_id, POG_NUM=post_title, OPOG_START_DT=post_content)
    db.session.add(new_post)
    db.session.commit()
    return redirect('/posts')
else:
    all_posts = CPR_INFO.query.all()
    return render_template('posts-2.html', posts=all_posts)



@app.route('/posts/new', methods = ['GET', 'POST'])
def new_post():
  if request.method == 'POST':
    post_id = request.form['DEAL_ID']
    post_title = request.form['POG_NUM']
    post_content = request.form['OPOG_START_DT']
    new_post = CPR_INFO(DEAL_ID=post_id, POG_NUM=post_title, OPOG_START_DT=post_content)
    db.session.add(new_post)
    db.session.commit()
    return render_template('posts-2.html', posts=all_posts)
else:
    return render_template('new_post_2.html')


@app.route('/posts/delete/<string:DEAL_ID>')
def delete(DEAL_ID):
  post = CPR_INFO.query.get_or_404(DEAL_ID)
  db.session.delete(post)
  db.session.commit()
  return redirect('/posts')

@app.route('/posts/edit/<string:DEAL_ID>', methods = ['GET', 'POST'])
def edit(DEAL_ID):
   post = CPR_INFO.query.get_or_404(DEAL_ID)

   if request.method == 'POST':
      post.DEAL_ID = request.form['DEAL_ID']
      post.POG_NUM = request.form['POG_NUM']
      post.OPOG_START_DT = request.form['OPOG_START_DT']
      db.session.commit()
      return redirect('/posts')
   else:
        return render_template('edit-2.html', post = post)


 if __name__ == "__main__":
    app.run(debug=True)

This part of my code fails.我的这部分代码失败了。 The URL is not rendered because the DEAL_ID is not read:由于未读取 DEAL_ID,因此未呈现 URL:

@app.route('/posts/delete/<string:DEAL_ID>')
def delete(DEAL_ID):
  post = CPR_INFO.query.get_or_404(DEAL_ID)
  db.session.delete(post)
  db.session.commit()
  return redirect('/posts')

I get the following error message on my HTML page:我在 HTML 页面上收到以下错误消息:

Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

在你的@app.route('/posts/delete/<string:DEAL_ID>')添加methods参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python @ app.route(“ / custom”),返回页不起作用 - Python @app.route(“/custom”), return page doesn't work flask —如何解决app.route / app.add_view_rule不适用于闭包的限制? - flask — how to get around limitation that app.route / app.add_view_rule doesn't work with closures? 装饰函数如何在flask / python中工作? (app.route) - how do decorated functions work in flask/python? (app.route) flask-我无法显示第二个app.route的输出 - flask - I can't get to show the output of the second app.route flask、@app.route(“/login') 还是@app.route(”/login/") 哪种形式的路由更好? - Which form of routing is better in flask, @app.route(“/login') or @app.route(”/login/")? 无法在 flask 应用程序的 @app.route 块中使用全局变量 - Not able to use global variables in a @app.route block in flask app 如何使用 Flask 修复 @app.route 语法错误? - How do I fix @app.route syntax error with Flask? Flask:为什么app.route()装饰器应该始终位于最外面? - Flask: why app.route() decorator, should always be the outermost? 如何在2个不同的app.route中使用Flask Flash消息 - How to use flask flash messages in 2 different app.route Flask Web开发通过app.route传递多个变量 - Flask web developments passing multiple variables thru app.route
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM