Toy_project #1

2023. 1. 10. 22:15TOY_PROJECT

@app.route("/api/link", methods=["POST"])
def link_post():
    title = request.form['title']
    url = request.form['url']
    tag = request.form['tag']
    author = request.form['author']
    image = request.form['image']

    count = list(db.links.find({}, {'_id': False}))
    id = len(count) + 1

    doc = {
        'id': id,
        'title': title,
        'url': url,
        'tag': tag,
        'author': author,
        'image': image
    }

    db.links.insert_one(doc)
    return jsonify({'msg': '링크 등록 완료!'})

항해 99를 시작하고 Toy_project를 시작했다.

 

프론트엔드와 백엔드의 연동 방법에 대해서만 어렴풋이 기억이 났지만, 어떠한 코드를 활용하는지 정확히 기억하지 못했다.

웹 개발 종합반 실습 내용을 통해

request.form[' ']

db.  .insert_one(  )

methods를 활용한 POST 코드를 작성했다.

@app.route("/api/link/<int:id>", methods=["PUT"])
def link_put(id):

    title = request.form['title']
    url = request.form['url']
    tag = request.form['tag']
    author = request.form['author']
    image = request.form['image']

    new_doc = {
        'id': id,
        'title': title,
        'url': url,
        'tag': tag,
        'author': author,
        'image': image
    }

    db.links.update_one({'id':int(id)},{'$set': new_doc})

    return jsonify({'msg': '링크 수정 완료!', 'path': id})

methods의 PUT은 처음 활용한 코드다.

id 외 나머지 정보를 모두 수정하려고

def ink_put(id):를 작성해 id를 기준으로 설정했다.

 

db.  .update_one({'  ':int(  )},{'$set':   })

 

* int : 받은 데이터를 숫자로 인식하도록 변경

* {'$set':    } 수정

 

'TOY_PROJECT' 카테고리의 다른 글

Read  (0) 2023.01.13
Create  (0) 2023.01.13
ajax  (0) 2023.01.13