๐Ÿฆ Python Online Store ๋งŒ๋“ค๊ธฐ 8ํŽธ - ์ƒํ’ˆ list ์กฐํšŒ

yohan-record[web]ยท2022๋…„ 8์›” 3์ผ
0

python onlinestore ๊ฐœ๋ฐœ์ผ์ง€

๋ชฉ๋ก ๋ณด๊ธฐ
8/25
post-thumbnail

๐ŸŽˆ Front End Developer ์˜ Back End ๋„์ „๊ธฐ ! ๐ŸŽˆ

์ด ๊ธ€์€ PROJECT LION : ํ˜ธ์ฝ”์น˜ ๊ฐ•์‚ฌ๋‹˜์˜ ๊ฐ•์˜๋ฅผ ๋“ค์œผ๋ฉฐ ์ฐธ๊ณ ํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

์ž‘์—… ์ค‘, ๊ฐœ๋ฐœ๊ณผ์ • ์ •๋ฆฌ ๋ฐ issue๊ฐ€ ๋˜์—ˆ๋˜ ๋ถ€๋ถ„๋“ค์„ ๊ณต์œ  ๋ฐ ๊ธฐ๋กํ•˜๊ณ ์ž ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

1. ์ƒํ’ˆ์กฐํšŒ

โœ ์ƒํ’ˆ๋“ฑ๋ก ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•˜๋‹ค. flask ์—์„œ ์ฃผ์†Œ๋กœ products/list(get) ์ ‘์† ํ•  ๋•Œ, products.html file์„ ์ „๋‹ฌ์‹œํ‚ฌ ๊ฒƒ์ด๋‹ค.

  1. API ์ƒ์„ฑ = product route ์˜ ์ฃผ์†Œ๋Š” /list(get ๋ฐฉ์‹์€ default ์ด๋ฏ€๋กœ ํ‘œ๊ธฐ์•ˆํ•จ) ๋กœ ์„ค์ •ํ•˜์˜€๊ณ , ํ•จ์ˆ˜๋ช…์€ get_products ๋กœ ์ •์˜ํ•˜์˜€๋‹ค.

์ฆ‰, list ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅ ์‹œ get_products ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ ๋˜๊ณ , ์ƒํ’ˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋•ก๊ฒจ์™€์•ผ ํ•œ๋‹ค.

๋ง๋ณด๋‹ค๋Š” ์—ญ์‹œ ์„ค๋ช…์ด๋ฏ€๋กœ ๊ทธ๋ฆผ์œผ๋กœ ํ˜๋Ÿฌ๊ฐ€๋Š” logic ์„ ๊ตฌ์ƒํ•ด ๋ณด๊ฒ ๋‹ค. ... ์‚ฌ์‹ค ๋‚˜๋งŒ ์•Œ์•„๋ณผ ๊ฑฐ ๊ฐ™๊ธด ํ•˜๋‹ค ^^;; ๐Ÿ˜…
๊ฐ„๋‹จํ•˜๊ฒŒ ๋ณด์ž๋ฉด, db > products > collection > document(์ƒํ’ˆ๋ช…, ๊ฐ€๊ฒฉ, ์ด๋ฏธ์ง€ ๋“ฑ๋“ฑ...) ์„ ๋ฝ‘์•„์™€์„œ list์— ๋ฟŒ๋ ค์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

๐Ÿ”– Product collection ์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์ž‘์„ฑํ•  ๋•Œ๋Š”, models > product file์—์„œ ์ž‘์„ฑํ•ด ์ค€๋‹ค.


  1. staticmethod ์ƒ์„ฑ
    = product collection ์•ˆ์— ์žˆ๋Š” ๋ชจ๋“  document ๋“ค์„ find() ๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.
    = ๊ทธ ํ›„, find ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑ์‹œ์ผœ์ค€๋‹ค.

  1. ์ƒํ’ˆ list ์ •๋ณด ์ถœ๋ ฅ์„ ์œ„ํ•œ html file ์ƒ์„ฑ( templates > products.py)
    = blueprint๋กœ products ๋ฅผ mapping ํ•ด ๋‘์—ˆ์œผ๋ฏ€๋กœ, /products/list ์ฃผ์†Œ ํ˜ธ์ถœ ์‹œ products.html file์˜ ๋‚ด์šฉ์ด ๋ณด์—ฌ์ง€๊ฒŒ ๋œ๋‹ค.

  1. product ์ •๋ณด ์ถœ๋ ฅ
    = product1 ๋ณ€์ˆ˜์— products(DB ์•ˆ documents ๋“ค์˜ ๋‚ด์šฉ์„ ๋‹ด์€ ๋ณ€์ˆ˜) ๋ฅผ ๋‹ด์•„์ค€๋‹ค.
    = html file์—์„œ products์— ์žˆ๋Š” ๋ฐฐ์—ด ์š”์†Œ๋“ค์„ ๊ฐœ์ˆ˜๋งŒํผ for๋ฌธ์„ ๋Œ๋ ค์„œ ํ•˜๋‚˜ํ•˜๋‚˜ ์ถœ๋ ฅ์„ ์‹œ์ผœ์ค€๋‹ค.
    = document ์š”์†Œ ํ•˜๋‚˜ํ•˜๋‚˜๊ฐ€ ์ถœ๋ ฅ์ด ๋˜์—ˆ๋‹ค.

๊ฒฐ๊ณผ๋ฌผ๊ณผ ๊ฐ™์ด db์— ์ €์žฅ๋œ list๋“ค์„ ์ถœ๋ ฅ์‹œ์ผœ์ฃผ์—ˆ๋‹ค.

๋‹ค์Œ์‹œ๊ฐ„์—๋Š” ์‹ค์ œ store์ฒ˜๋Ÿผ ๋ณด์ด๋„๋ก html ๊ตฌ์กฐ ๋ฐ css ์ž‘์—…์„ ํ†ตํ•˜์—ฌ list ์ฒ˜๋Ÿผ ๋ณด์ด๊ฒŒ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ๋‹ค!

profile
๐Ÿฑโ€๐Ÿ Front End Developer

0๊ฐœ์˜ ๋Œ“๊ธ€