App.route не работает в Flask для одного метода - PullRequest
0 голосов
/ 21 февраля 2020

Вот мой код:

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)

Эта часть моего кода не работает. URL не отображается, поскольку DEAL_ID не читается:

@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.

1 Ответ

2 голосов
/ 21 февраля 2020

Добавить methods параметр на вашем @app.route('/posts/delete/<string:DEAL_ID>')

...