Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 烧瓶问题:重定向行为不符合预期_Python_Html_Flask - Fatal编程技术网

Python 烧瓶问题:重定向行为不符合预期

Python 烧瓶问题:重定向行为不符合预期,python,html,flask,Python,Html,Flask,我正在做我的第一个flask实践项目,不明白为什么重定向不起作用。“帖子”页面有博客帖子,位于/post/edit/id的编辑页面应该更新它们。表单似乎正常工作,但在保存时,它会转到/posts/edit/posts/edit/1,而不仅仅是/posts,即使/posts在重定向中传递。以下是主要代码: 从flask导入flask,呈现\u模板,请求,重定向 从flask_sqlalchemy导入sqlalchemy 从日期时间导入日期时间 app=烧瓶(名称) app.config['SQLA

我正在做我的第一个
flask
实践项目,不明白为什么重定向不起作用。“帖子”页面有博客帖子,位于
/post/edit/id
的编辑页面应该更新它们。表单似乎正常工作,但在保存时,它会转到
/posts/edit/posts/edit/1
,而不仅仅是
/posts
,即使
/posts
重定向中传递。以下是主要代码:

从flask导入flask,呈现\u模板,请求,重定向
从flask_sqlalchemy导入sqlalchemy
从日期时间导入日期时间
app=烧瓶(名称)
app.config['SQLALCHEMY\u DATABASE\u URI']='sqlite:///posts.db'
db=SQLAlchemy(应用程序)
Blogpost类(db.Model):
id=db.Column(db.Integer,主键=True)
author=db.Column(db.String(25),nullable=False,默认值='N/A')
title=db.Column(db.String(30),null=False)
content=db.Column(db.String(150),nullable=False)
发布日期=db.Column(db.DateTime,默认值=DateTime.utcnow)
定义报告(自我):
返回str(self.id)
@应用程序路径(“/home”)
@应用程序路径(“/”)
@app.route('/posts',methods=['GET',POST'])
def hello():
如果request.method==“POST”:
post_title=请求。表单['title']
post_author=request.form['author']
post_content=request.form['content']
new_post=Blogpost(title=post_title,author=post_author,content=post_content)
db.session.add(新帖子)
db.session.commit()
返回重定向(“/posts”)
其他:
all_posts=Blogpost.query.order_by(Blogpost.date_posted).all()
返回呈现模板('posts.html',posts=all_posts)
@app.route(“/posts/delete/”)
def删除(id):
post=Blogpost.query.get\u或\u 404(id)
db.session.delete(post)
db.session.commit()
返回重定向(“/posts”)
@app.route('/posts/edit/',methods=['GET','POST'])
def编辑(id):
post=Blogpost.query.get\u或\u 404(id)
如果request.method==“POST”:
post.title=request.form['title']
post.author=request.form['author']
post.content=request.form['content']
db.session.commit()
返回重定向(“/posts”)
其他:
返回呈现模板('edit.html',post=post)
@app.route(“/about”)
def about():
返回渲染模板('aboutus.html',title='About')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run(debug=True)
下面是html:

{%extends'base.html%}
{%block content%}
编辑文章

标题:
作者:
职位:
{%endblock%}
基本html文件如下所示:


{%if title%}
{{title}}
{%else%}
欢迎
{%endif%}
{%block content%}
{%endblock%}
{%block content2%}
{%endblock%}
重定向示例:

from flask import url_for

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

@app.route('/sample')
def Sample():
    return redirect(url_for('Index'))
当用户输入“/sample”路径时,它被定向到/index路径