๐Ÿฆ Python Online Store ๋งŒ๋“ค๊ธฐ 6ํŽธ - ์ƒํ’ˆ ์ด๋ฏธ์ง€ upload

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

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

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

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

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

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

1. img upload

โœ ์ด์ „ ์‹œ๊ฐ„์—๋Š” ํŒ๋งคํ•˜๊ณ ์ž ํ•˜๋Š” ์ƒํ’ˆ์˜ ๊ณ ์œ  ๊ฐ’ ๋ฐ ์ •๋ณด๋“ค์˜ ๊ฐ’์„ db์— ์ €์žฅํ•˜์˜€๋‹ค.

์ถ”๊ฐ€์ ์œผ๋กœ list์— ๋ฟŒ๋ ค์งˆ thumbnail img ๋ฐ view page์˜ img ๋„ ํŒŒ์ผ ๊ฒฝ๋กœ๋ฅผ ์ƒ์„ฑํ•œ ํ›„ ์ง€์ •๋œ filename์œผ๋กœ ์ €์žฅํ•˜์—ฌ ๊ฐ’์„ ๋„˜๊ฒจ๋ณด๋„๋ก ํ•˜๊ฒ ๋‹ค.

  1. request file ๊ฒฝ๋กœ ์ƒ์„ฑ(thumbnail ๋ฐ detail img)
    = ํ•ด๋‹น img file์„ upload ํ•˜๊ฒŒ ๋˜๋ฉด, request.files ๋ผ๋Š” ๊ณณ์— thumbnail_img ๋ฐ detail_img ๋ผ๋Š” file์˜ ์ •๋ณด๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.

    file ์ •๋ณด์˜ ๊ฒฝ์šฐ ์–ด๋”˜๊ฐ€์— ์ €์žฅ ํ•ด ์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, file์€ text๊ฐ€ ์•„๋‹Œ ๋ง ๊ทธ๋Œ€๋กœ file ํ˜•ํƒœ์ด๊ธฐ ๋•Œ๋ฌธ์— python file ๊ณผ ๊ฐ™์€ file ํ˜•ํƒœ๋กœ ์ €์žฅํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.


  1. img_file upload ์œ„ํ•œ ํ•จ์ˆ˜ ์ƒ์„ฑ
    = ๊ฐ ์ฝ”๋“œ์˜ ํ๋ฆ„์— ๋งž๊ฒŒ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์„์ฒ˜๋ฆฌ ํ•˜์˜€์ง€๋งŒ ๋‹ค์‹œํ•œ๋ฒˆ ๋ณต์Šตํ•  ๊ฒธ ์ˆœ์„œ๋ฅผ ๋‚˜์—ดํ•˜๋ฉฐ ์ฝ”๋“œ๋ฅผ ์žฌ์ž‘์„ฑ ํ•ด๋ณด๊ฒ ๋‹ค.

๐Ÿงธ ํ•จ์ˆ˜ ๋‚ด๋ถ€ ์ˆœ์„œ ๐Ÿงธ
2-1. upload ์‹œํ‚จ ๋‚ ์งœ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด, timestamp ํ•จ์ˆ˜ ์‚ฌ์šฉ
2-2. ์ƒ์„ฑ๋œ file ์ด๋ฆ„์—๋Š” ์ƒ์„ฑํ•œ ๋‚ ์งœ์™€ ์ด๋ฆ„์„ ์ถ”๊ฐ€ํ•œ๋‹ค.
2-3. upload ์‹œํ‚ค๊ณ ์ž ํ•˜๋Š” path ์„ค์ •
2-4. directory ์กด์žฌ ์œ ๋ฌด๋ฅผ ํŒŒ์•…ํ•˜์—ฌ ํ•ด๋‹น directory์— path ๋ฐ filename upload
2-5. path ๊ฒฝ๋กœ์— file save
2-6. ์ €์žฅ๋œ path ๊ฒฝ๋กœ > img file mongoDB upload


  1. upload file ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐ img ๊ฐ’ db์ „๋‹ฌ
    = upload_file ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐ ์š”์ฒญํ•œ img ๊ฐ’์„ thumnail_img_url ๋ฐ detail_img_url ๋ณ€์ˆ˜์— ์ €์žฅ ํ›„, db ์— ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด insert_one ๋‚ด๋ถ€์— ๊ฐ’์„ ์ „์†กํ•ด์ค€๋‹ค.

  1. ์š”์ฒญ ํ›„ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’ db ์ „์†ก
    = db์— ๋ณด๋‚ด์ค„ ๊ฐ’์„ ๋ฐ›์•„์„œ, insert ์‹œ์ผœ์ค€๋‹ค(user๊ฐ’์€ ์ž„์˜๋กœ admin์œผ๋กœ ์„ธํŒ…ํ–ˆ์œผ๋ฉฐ, ์ƒ์„ฑ๋‚ ์งœ ๋ฐ ์ˆ˜์ •๋‚ ์งœ๋Š” filename์„ ์ €์žฅํ•  ๋•Œ ์ฒ˜๋Ÿผ timestamp ๊ฐ’์„ ์ •์ˆ˜ type์œผ๋กœ ์ƒ์„ฑํ•˜์—ฌ db์— ๋‹ด์•„์ฃผ์—ˆ๋‹ค.)

  1. datetime, secure_filename, os ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
    = models > product.py file๋„ datetime ์„ ์‚ฌ์šฉํ–ˆ์œผ๋ฏ€๋กœ, ๋™์ผํ•˜๊ฒŒ datetime import ํ•„์š”

  1. upload ํ…Œ์ŠคํŠธ
    = Insomnia ์ ‘์† ํ›„, file ํ˜•ํƒœ๋กœ thumbnail img ๋ฐ detail img ์ฒจ๋ถ€ ํ›„ ์ „์†ก

  1. static/uploads folder ์ƒ์„ฑ ํ™•์ธ

  1. MongoDB ์ „์†ก ๊ฐ’ ํ™•์ธ

์ด๋กœ์จ img file์ด db์— ์ •์ƒ์ ์œผ๋กœ ์ €์žฅ ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค ๐Ÿ˜Ž

์ƒํ’ˆ๋“ฑ๋ก API์— ๋Œ€ํ•œ ๊ตฌํ˜„์„ ์™„๋ฃŒํ•˜์˜€๋‹ค!

ํ˜„์—…์—์„œ๋Š” fe ์—…๋ฌด๋งŒ ์ˆ˜ํ–‰ํ•˜๊ณ  ์žˆ์–ด, ์‹ค์งˆ์ ์œผ๋กœ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ๋„˜๊ธฐ๋Š” ์ž‘์—…์€ ์ฒ˜๋ฆฌํ•ด ๋ณด์ง€ ๋ชปํ–ˆ๋Š”๋ฐ, ์‹ค์ œ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ๊ฐ’์„ ๋„˜๊ธฐ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด๋ณด๋‹ˆ ์žฌ๋ฐŒ๋Š” ๊ฒƒ ๊ฐ™๋‹ค. (์•„์ง ์ฝ”๋“œ๋Š” ์†์‰ฝ๊ฒŒ ์ž‘์„ฑํ•˜์ง€๋Š” ๋ชปํ•  ๊ฒƒ ๊ฐ™๋‹ค ๐Ÿ˜‚)

๋‹ค์Œ์‹œ๊ฐ„์—๋Š” ์ƒํ’ˆ๋“ฑ๋ก page๋ฅผ ๊ตฌํ˜„ํ•˜์—ฌ ์‹ค์ œ web browser์—์„œ ๋“ฑ๋ก์ด ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•ด๋ณด๊ฒ ๋‹ค.

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

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