问题描述
我目前正在使用python开发烧瓶,到目前为止,我还没有问题,但是现在我有一个问题,无法访问更新/删除帖子页面。
登录帐户后,我的帖子应该看起来像这样,以便我可以更新或删除帖子。我的编码没有错误。
但是,我的现在看起来像这样。
下面的链接是我正在制作的视频。
https://www.youtube.com/watch?v=Wfx4YBzg16s&list=PL-osiE80TeTs4UjLw5MM6OjgkjFeUxCYH&index=11
起初,我以为可能是打字问题,所以我从他的github数据中复制并粘贴了所有内容。 但是,我仍然无法访问我的网站上的那些页面。另外,我也仔细检查了文件位置。
https://github.com/CoreyMSchafer/code_snippets/tree/master/Python/Flask_Blog/11-Blueprints
即使我每次通过python run.py命令激活flaskblog文件时,都会在终端上收到警告,但我认为这与问题无关。
py:834: FSADeprecationWarning: sqlALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
(填充树)
├── flaskblog
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── config.cpython-37.pyc
│ │ └── models.cpython-37.pyc
│ ├── avatar.png
│ ├── config.py
│ ├── errors
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ └── handlers.cpython-37.pyc
│ │ └── handlers.py
│ ├── main
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ └── routes.cpython-37.pyc
│ │ └── routes.py
│ ├── models.py
│ ├── posts
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ ├── forms.cpython-37.pyc
│ │ │ └── routes.cpython-37.pyc
│ │ ├── forms.py
│ │ └── routes.py
│ ├── site.db
│ ├── static
│ │ ├── main.css
│ │ └── profile_pics
│ │ ├── 47dc468c38f353b3.png
│ │ ├── 82f6833fbe6f478b.png
│ │ ├── a9472a1954a330fa.jpg
│ │ ├── abc63fb3d96d144c.jpg
│ │ ├── default.jpg
│ │ └── large.jpg
│ ├── templates
│ │ ├── about.html
│ │ ├── account.html
│ │ ├── create_post.html
│ │ ├── errors
│ │ │ ├── 403.html
│ │ │ ├── 404.html
│ │ │ └── 500.html
│ │ ├── home.html
│ │ ├── layout.html
│ │ ├── login.html
│ │ ├── post.html
│ │ ├── register.html
│ │ ├── reset_request.html
│ │ ├── reset_token.html
│ │ └── user_posts.html
│ └── users
│ ├── __init__.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ ├── forms.cpython-37.pyc
│ │ ├── routes.cpython-37.pyc
│ │ ├── utilis.cpython-37.pyc
│ │ └── utils.cpython-37.pyc
│ ├── forms.py
│ ├── routes.py
│ └── utils.py
├── run.py
├── users.sqlite3
└── venv
(flaskblog / posts / routes.py)
from flask import (render_template,url_for,flash,redirect,request,abort,Blueprint)
from flask_login import current_user,login_required
from flaskblog import db
from flaskblog.models import Post
from flaskblog.posts.forms import PostForm
posts = Blueprint('posts',__name__)
@posts.route("/post/new",methods=['GET','POST'])
@login_required
def new_post():
form = PostForm()
if form.validate_on_submit():
post = Post(title=form.title.data,content=form.content.data,author=current_user)
db.session.add(post)
db.session.commit()
flash('Your post has been created!','success')
return redirect(url_for('main.home'))
return render_template('create_post.html',title='New Post',form=form,legend='New Post')
@posts.route("/post/<int:post_id>")
def post(post_id):
post = Post.query.get_or_404(post_id)
return render_template('post.html',title=post.title,post=post)
@posts.route("/post/<int:post_id>/update",'POST'])
@login_required
def update_post(post_id):
post = Post.query.get_or_404(post_id)
if post.author != current_user:
abort(403)
form = PostForm()
if form.validate_on_submit():
post.title = form.title.data
post.content = form.content.data
db.session.commit()
flash('Your post has been updated!','success')
return redirect(url_for('posts.post',post_id=post.id))
elif request.method == 'GET':
form.title.data = post.title
form.content.data = post.content
return render_template('create_post.html',title='Update Post',legend='Update Post')
@posts.route("/post/<int:post_id>/delete",methods=['POST'])
@login_required
def delete_post(post_id):
post = Post.query.get_or_404(post_id)
if post.author != current_user:
abort(403)
db.session.delete(post)
db.session.commit()
flash('Your post has been deleted!','success')
return redirect(url_for('main.home'))
(flaskblog / templates / post.html)
{% extends "layout.html" %}
{% block content %}
<article class="media content-section">
<img class="rounded-circle article-img" src="{{ url_for('static',filename='profile_pics/' + post.author.image_file) }}">
<div class="media-body">
<div class="article-Metadata">
<a class="mr-2" href="{{ url_for('users.user_posts',username=post.author.username) }}">{{ post.author.username }}</a>
<small class="text-muted">{{ post.date_posted.strftime('%Y-%m-%d') }}</small>
{% if post.author == current_user %}
<div>
<a class="btn btn-secondary btn-sm mt-1 mb-1" href="{{ url_for('posts.update_post',post_id=post.id) }}">Update</a>
<button type="button" class="btn btn-danger btn-sm m-1" data-toggle="modal" data-target="#deleteModal">Delete</button>
</div>
{% endif %}
</div>
<h2 class="article-title">{{ post.title }}</h2>
<p class="article-content">{{ post.content }}</p>
</div>
</article>
<!-- Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="deleteModalLabel">Delete Post?</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<form action="{{ url_for('posts.delete_post',post_id=post.id) }}" method="POST">
<input class="btn btn-danger" type="submit" value="Delete">
</form>
</div>
</div>
</div>
</div>
{% endblock content %}
(flaskblog/__init__.py)
from flask import Flask
from flask_sqlalchemy import sqlAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
from flask_mail import Mail
from flaskblog.config import Config
db = sqlAlchemy()
bcrypt = Bcrypt()
login_manager = LoginManager()
login_manager.login_view = 'users.login'
login_manager.login_message_category = 'info'
mail = Mail()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
sqlALCHEMY_TRACK_MODIFICATIONS = True
db.init_app(app)
bcrypt.init_app(app)
login_manager.init_app(app)
mail.init_app(app)
from flaskblog.users.routes import users
from flaskblog.posts.routes import posts
from flaskblog.main.routes import main
from flaskblog.errors.handlers import errors
app.register_blueprint(users)
app.register_blueprint(posts)
app.register_blueprint(main)
app.register_blueprint(errors)
with app.app_context():
db.create_all()
return app
解决方法
这是我愚蠢的错误。我没有以该帖子的用户身份登录。