python - App.route 在 Flask 中不适用于一种方法
问题描述
这是我的代码:
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)
我的这部分代码失败了。由于未读取 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')
我在我的 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.
解决方案
在您的位置添加methods
参数@app.route('/posts/delete/<string:DEAL_ID>')
推荐阅读
- intellij-idea - 阻止 IntelliJ 在水平拆分视图中两次打开同一个文件
- android - CoordinatorLayout 上的自定义小吃吧上的文本被截断
- mysql - MySQL 使用 LIMIT 或 WHERE 删除记录
- c - 设备驱动程序中的哪些数据可以在进程之间共享?
- javascript - 将数组更改为小写
- ruby-on-rails - Selenium_chrome 不可见;总是无头
- javascript - 为什么 NFKC 对所有数字进行归一化不起作用?
- firebase - 如何将 Firestore 日期对象映射到 elasticsearch 中的日期
- java - Java 中的 OOP Hello World
- node.js - 无法连接到 Dockerized Express HTTPS 服务器