大佬教程收集整理的这篇文章主要介绍了405 Method Not Allowed 当提交表单到 Flask 时,即使路由有 ['GET', 'POST'],大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
遇到“405 Method Not Allowed”错误代码的问题。填写表单字段并提交后,浏览器会抛出 405 Method Not Allowed 错误代码。我该如何解决这个问题?
from flask import Flask,render_template,request,redirect
from flask_sqlalchemy import sqlAlchemy
from datetiR_177_11845@e import datetiR_177_11845@e
app = Flask(__name__)
app.config['sqlALCHEmy_database_URI'] = 'sqlite:///posts.db'
db = sqlAlchemy(app)
class BlogPost(db.Model):
ID = db.column(db.Integer,priMary_key=TruE)
title = db.column(db.String(100),@R_@R_675_11263@_1352@)
content = db.column(db.Text,@R_@R_675_11263@_1352@)
author = db.column(db.String(20),@R_@R_675_11263@_1352@,default='N/A')
date_posted = db.column(db.datetiR_177_11845@e,default=datetiR_177_11845@e.utcNow)
all_posts = [
{
'title': 'Post 1','content': 'This is the content of post 1. Lalalala','author': 'Jon'
},{
'title': 'Post 2','content': 'This is the content of post 2. Blahahahah'
}
]
@app.route('/posts',methods=['GET','POST'])
def posts():
if request.method == 'POST':
post_title = request.form['title']
post_content = request.form['content']
new_post = BlogPost(title=post_title,content=post_content,author='Jon')
db.session.add(new_post)
db.session.commit()
return redirect('/posts')
else:
all_posts = BlogPost.query.order_by(BlogPost.date_posted).all()
return render_template('posts.HTML',posts=all_posts)
posts.HTML
:
{% extends 'base.HTML' %}
{% block head %}
<title>posts</title>
{% endblock %}
{% block body %}
<h1>All posts</h1>
<hr>
<h2>Create New Blog Post</h2>
<form action='/posts' method='POST'>
title: <input type='text' name='title' ID='title'>
<br>
Post: <input type='text' name='content' ID='content'>
<br>
<input type='submit' value='Post'>
</form>
<hr>
{% for post in posts %}
<h2>{{ post.title }}</h2>
{% if post.author %}
<h3>By: {{ post.author }}</h3>
{% else %}
<h3>By: N/A</h3>
{% endif %}
<p>{{ post.content }}</p>
{% endfor %}
{% endblock %}
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的405 Method Not Allowed 当提交表单到 Flask 时,即使路由有 ['GET', 'POST']全部内容,希望文章能够帮你解决405 Method Not Allowed 当提交表单到 Flask 时,即使路由有 ['GET', 'POST']所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。