TIL Python Basics Day 67 - Blog Capstone Project Part 3: RESTful Routing

3373 단어 pythonpython

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

http://cdn.ckeditor.com/

        {{ ckeditor.load() }}
        {{ ckeditor.config(name='body') }}

좋은 웹페이지 즐겨찾기