我正在尝试实现我在社交媒体平台上看到的功能,在这种情况下,例如,未登录的用户想要发表或发表评论,出现一个登录弹出窗口(在我的情况下为模式),他们预期会登录((如果没有帐户,该模式还会提示他们注册),然后将他们重定向到该帖子,并且现在可以发表评论。
这是我的家庭路线查看功能。
@app.route("/",methods=['GET','POST'])
@app.route("/home",methods=['GET','POST'])
def home():
posts = Pitch.query.all()
comments = Comment.query.all()
registerForm = Register()
if registerForm.validate_on_submit():
hashed_password = bcrypt.generate_password_hash(registerForm.password.data).decode('utf-8')
user = User(username = registerForm.username.data, email = registerForm.email.data, password = hashed_password)
db.session.add(user)
db.session.commit()
flash(f'Your account has been created! You are now able to login!','success')
mail_message("Welcome to watchlist","templates/email/welcome_user",user.email,user=user)
return redirect(url_for('home'))
form = Login()
if form.validate_on_submit():
user = User.query.filter_by(email=form.email.data).first()
if user and bcrypt.check_password_hash(user.password,form.password.data):
login_user(user, remember=form.remember.data)
next_page = request.args.get('next')
return redirect(next_page) if next_page else redirect(url_for('circles'))
else:
flash('Login Unsuccessful. Please check email and password','danger')
return render_template('home.html', title='login',form=form, registerForm=registerForm ,posts=posts, comments=comments)
这是我个人的帖子查看功能
@app.route("/post/<int:post_id>",methods=['GET','POST'])
def post(post_id):
review= CommentForm()
comments = Comment.query.all()
post = Pitch.query.get_or_404(post_id)
if review.validate_on_submit():
comment = Comment(body= review.comment.data,post_id=post.id )
db.session.add(comment)
db.session.commit()
flash('Your post has been created!','success')
return redirect(url_for('post',post_id=post.id))
return render_template('post.html',title = post.title, post=post,review=review, comments = comments )
这是我的主页模板摘要:
{% for post in posts %}
<div id="homejumbotron" class="jumbotron">
<div class="container-fluid">
<div class="row">
<img id="avatar" src="{{ url_for('static',filename='images/' + post.author.image_file) }}" alt=""
class="rounded-circle col-md-2">
<div id="postintro" class="col-md-10 ">
<a id="authorname" href="#" class="row mr-2">{{post.author.username}}</a>
<small class="text-muted row" id="date">
{{post.date_posted.strftime('%Y-%m-%d')}}
</small>
</div>
</div>
</div>
<hr>
<a id="title" href="{{url_for('post',post_id=post.id)}}">{{ post.title }}</a>
<div class="footer">
<p class="article-content">{{ post.content }}</p>
</div>
<hr>
{% if current_user.is_authenticated %}
<a href="{{url_for('post',post_id=post.id)}}"><i class="far fa-comment-alt"></i><small>Leave review</small></i></a>
{% else %}
<!--if not authenticated,pop up login modal,then redirect back to the post you were trying to view-->
<a href="{{url_for('post',post_id=post.id)}}" data-toggle="modal" data-target="#modalLoginForm"><i class="far fa-comment-alt"></i><small>Leave review</small></i></a>
{% endif %}
</div>
{%endfor%}
由于模态表单提交会重定向到圈子模板,因此当前直接将其重定向到我的圈子模板。对此有何建议?