나는 Flask 프레임 워크를 사용하여 파이썬으로 블로그를 만들고 있습니다.플라스크 웹 앱의 URL 스키마에서 가능한 개선
내가 게시물을 표시보기/URL을 다음 있습니다 : 위의 코드에서
@post_blueprint.route('/post/<int:year>/<int:month>/<title>', methods=['GET'])
def get_post(year, month, title):
try:
title = title.replace('-', ' ')
post = Post.query.filter(Post.title == title).one()
post.comment_form = UserCommentForm()
except NoResultFound:
raise
return render_template('show_single_post.html', post=post)
을, 또한 사용자가 댓글을 게시 할 수 있도록하는 코멘트 양식을 전달하고있다. 그 후 나는이 게시물 요청을 수신보기/URL을 썼다 :
위의보기에서@post_blueprint.route('post/<int:post_id/comment/add/', mehtods=['POST'])
def add_comment(post_id):
post = Post.query.get(post_id)
if not post:
raise Exception
if request.method == 'POST':
form = UserCommentForm(request.form)
if form.validate():
try:
user = User.query.\
filter(User.email == form.email.data).one()
except NoResultFound:
user = User(form.username.data, form.email.data,
form.website.data or None)
try:
comment = Comment(form.comment.data, post_id)
comment.author = user
db_session.add(comment)
db_session.commit()
except IntegrityError:
raise
return redirect(url_for('.get_post', year=post.posted_on.year,
month=post.posted_on.month,
title=post.title.replace(' ','-')
)
)
, 나는 확실히 게시물이 존재하게 DB에서 포스트 객체에 접근하고있다. 존재하지 않는 사용자를 만든 다음 주석을 저장합니다.
지금 내 문제는 전체 게시물 개체에 액세스하는 대신 카운트 쿼리를 실행하여 게시물 존재 여부를 테스트 할 수 있지만 전체 개체를 가져야하므로 게시 특성이 필요하기 때문입니다.
아마도 내 URL 구성표가 좋지 않거나 여기에서 할 수있는 좋은 점이 있다고 느낍니다.
개선 할 수있는 방법과 개선 방법은 누구에게 말해 줄 수 있습니까?