TIL Python Basics Day 67 - Blog Capstone Project Part 3: RESTful Routing
Blog Capstone Project Part 3 - RESTful Routing
Goal: Getting api to retrieving from database
Unable to Connect Error
Turn on debug mode to find out the problem
if __name__ == "__main__": app.run(debug=True) #app.run(host='0.0.0.0', port=5000)
-> problem was about non-existing keyword_argument on route
-> start with debug=True, to figure out the error
Read A Record
All Data
db.session.query(BlogPost).all()
By Primary Key
book_id = 1
db.session.query(Book).get(book_id)
Dummy Route
When href got a route that doesn't exist yet, it will cause error. Simply put dummy route to quiet it for now.
post.html
<a class="btn"
href="{{url_for('edit_post',post_id=post.id)}}">Edit Post</a>
main.py
@app.route("/edit_post")
def edit_post():
pass
New_post() & Edit_post() -> make_post.html / Form pre-populated
You need to pass your object to the form when you create it.
edit_form = CreatePostForm( title=post.title...)```
@app.route("/edit-post/<post_id>", methods=["GET","POST"])
def edit_post(post_id):
edit_post = db.session.query(BlogPost).get(post_id)
edit_form = CreatePostForm(
title=post.title,
subtitle=post.subtitle,
img_url=post.img_url,
author=post.author,
body=post.body
)
return render_template("make-post.html", edit_form=edit_form, is_edit=True)
@app.route("/new-post", methods=["GET","POST"])
def new_post():
new_form = CreatePostForm()
return render_template("make-post.html", new_form=new_form, is_edit=False)
{% if is_edit: %}
{{ wtf.quick_form(edit_form) }}
{% else: %}
{{ wtf.quick_form(new_form) }}
{% endif %}
CKeditior
{{ ckeditor.load() }}
{{ ckeditor.config(name='body') }}
Author And Source
이 문제에 관하여(TIL Python Basics Day 67 - Blog Capstone Project Part 3: RESTful Routing), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@daylee/TIL-Python-Basics-Day-67-Blog-Capstone-Project-Part-3-RESTful-Routing저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)