簡體   English   中英

App.route 在 Flask 中不適用於一種方法

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

這是我的代碼:

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.

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM