728x90
Backend/๐Ÿ”ต Django

Backend/๐Ÿ”ต Django

    [Django] ์žฅ๊ณ  ์ •์ ํŒŒ์ผ ๊ด€๋ฆฌํ•˜๊ธฐ - Static, Media

    ์ •์ ํŒŒ์ผ ์ด๋ž€? ์ •์ ํŒŒ์ผ์€ HTML์„ ์ œ์™ธํ•˜๊ณ  ์›น ํŽ˜์ด์ง€๋ฅผ ๋ Œ๋”๋ง ํ•  ๋•Œ ํ•„์š”ํ•œ ์ถ”๊ฐ€์ ์ธ ํŒŒ์ผ์„ ์˜๋ฏธํ•œ๋‹ค. ์„œ๋ฒ„์˜ ๋ฐ์ดํ„ฐ๋“ค์ด ์–ด๋Š์ •๋„ ๊ฐ€๊ณต๋œ ํ›„ ๋ณด์—ฌ์ง€๋Š” ๋™์ ํŒŒ์ผ๊ณผ ๋‹ฌ๋ฆฌ, ์„œ๋ฒ„์— ๋ฏธ๋ฆฌ ์ €์žฅ๋˜์–ด ์žˆ์–ด์•ผํ•˜๋ฉฐ, ์„œ๋ฒ„์— ์ €์žฅ๋œ ๊ทธ๋Œ€๋กœ๋ฅผ ์„œ๋น„์Šคํ•œ๋‹ค. ์žฅ๊ณ ์—์„œ ์ •์ ํŒŒ์ผ์€ ์•„๋ž˜์˜ ๋‘๊ฐ€์ง€ ํŒŒ์ผ๋กœ ๋ถ„๋ฅ˜๋  ์ˆ˜ ์žˆ๋‹ค. Static ํŒŒ์ผ : ์ด๋ฏธ์ง€, Javascript, CSS์ฒ˜๋Ÿผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์„œ๋ฒ„๋ฅผ ๊ฐœ๋ฐœํ•  ๋•Œ ๋ฏธ๋ฆฌ ๋„ฃ์–ด ์ค€๋น„ํ•ด ๋†“์€ ํŒŒ์ผ๋กœ, ์„œ๋น„์Šค ์ค‘์— ์ถ”๊ฐ€๋˜๊ฑฐ๋‚˜ ์ˆ˜์ •๋˜์ง€ ์•Š๊ณ  ๊ณ ์ •๋˜์–ด ์žˆ์Œ Media ํŒŒ์ผ : ์‚ฌ์šฉ์ž๊ฐ€ ์—…๋กœ๋“œ ํ•  ์ˆ˜ ์žˆ๋Š” ํŒŒ์ผ๋กœ, ํŒŒ์ผ ์ž์ฒด๋Š” ๊ณ ์ •๋˜์–ด ์žˆ์ง€๋งŒ ์–ธ์ œ ์–ด๋–ค ํŒŒ์ผ์ด ์ œ๊ณต๋  ์ง€๋Š” ์˜ˆ์ธกํ•  ์ˆ˜ ์—†์Œ Static ํŒŒ์ผ ๊ด€๋ฆฌ 1. app ํด๋” ์•„๋ž˜ static ํด๋” ์ƒ์„ฑ ํ›„ ์ด๋ฏธ์ง€/Javascript/CSS..

    [Django] ์žฅ๊ณ  CRUD ๊ตฌํ˜„ - ๊ฒŒ์‹œํŒ ๊ธฐ๋Šฅ

    CRUD ๋ž€? Create : DB์— ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•จ Read : DB์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ด Update : DB์˜ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•จ Delete : DB์˜ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•จ Create - ๋ฐ์ดํ„ฐ ์ƒ์„ฑ - GET : ๋ฐ์ดํ„ฐ๋ฅผ ์–ป๊ธฐ ์œ„ํ•œ ์š”์ฒญ - ๋ฐ์ดํ„ฐ๊ฐ€ URL์— ๋‚˜ํƒ€๋‚จ # ex) ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ search def search(request): keyword = request.GET['search_value'] POST : ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ์š”์ฒญ ๋ฐ์ดํ„ฐ๊ฐ€ URL์— ๋‚˜ํƒ€๋‚˜์ง€ ์•Š์Œ csrf ๊ณต๊ฒฉ ๋ฐฉ์ง€ ํ•„์š” # views.py def create(request): new_blog = Blog() # ๋ฐ์ดํ„ฐ ์ €์žฅ์„ ์œ„ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ new_blog.title = request.POST['title'] ne..

    [Django] ์žฅ๊ณ  models ๋ฐ DB ์„ค๊ณ„ - Migration, Admin

    Migration์ด๋ž€? Django์—์„œ Migration์ด๋ž€, ์‰ฝ๊ฒŒ ๋งํ•ด python์œผ๋กœ ์ž‘๋™ํ•˜๋Š” ์›น ํ”„๋ ˆ์ž„์›Œํฌ Django์—์„œ python์œผ๋กœ ์งœ์—ฌ์ง„ code๋ฅผ DB๊ฐ€ ์•Œ์•„๋“ค์„ ์ˆ˜ ์žˆ๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ๋Š” ์ž‘์—…์ด๋‹ค. model์˜ ๋ณ€๊ฒฝ๋œ ๋‚ด์šฉ์„ DB ์Šคํ‚ค๋งˆ์— ์ ์šฉ์‹œ์ผœ ์ค€๋‹ค. 1. Models ์ •์˜ํ•˜๊ธฐ ์–ด๋–ค ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์€์ง€ class๋กœ ์ •์˜ํ•œ๋‹ค. ๋ธ”๋กœ๊ทธ ๋ฐ์ดํ„ฐ๋กœ ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. from django.db import models # Create your models here. class Blog(models.Model): # title ๋ณ€์ˆ˜๋Š” ์ตœ๋Œ€ ๊ธธ์ด 200์ธ ์งง์€ ๋ฌธ์ž์—ด ํ˜•์‹์œผ๋กœ ์ •์˜ title = models.CharField(max_length=200) # writer ๋ณ€์ˆ˜๋Š” 100..

    [Django] ์žฅ๊ณ  ์‹ค์Šต/๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์ค€๋น„ํ•˜๊ธฐ - ํ”„๋กœ์ ํŠธ ๋ฐ ์•ฑ ์ƒ์„ฑ

    1. ๊ฐ€์ƒํ™˜๊ฒฝ ์ƒ์„ฑ - ๊ฐ€์ƒํ™˜๊ฒฝ์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์—ฌ๋Ÿฌ ๊ฐœ ์ง„ํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•˜์—ฌ ์„ค์ • $ python -m venv ๊ฐ€์ƒํ™˜๊ฒฝ๋ช… (window) $ python3 -m venv ๊ฐ€์ƒํ™˜๊ฒฝ๋ช… (mac) 2. ๊ฐ€์ƒํ™˜๊ฒฝ ์‹คํ–‰(ํ™œ์„ฑํ™”) $ source ๊ฐ€์ƒํ™˜๊ฒฝ๋ช…/Scripts/activate (window) $ source ๊ฐ€์ƒํ™˜๊ฒฝ๋ช…/bin/activate (mac) 3. Django ํŒจํ‚ค์ง€ ์„ค์น˜ $ pip install django 4. ํ”„๋กœ์ ํŠธ ํŒŒ์ผ ์ƒ์„ฑ $ django-admin startproject ํ”„๋กœ์ ํŠธ๋ช… 5. App ์ƒ์„ฑ - ์•ฑ์€ Django ํ”„๋กœ์ ํŠธ๋ฅผ ์ด๋ฃจ๋Š” ์ž‘์€ ๋‹จ์œ„ - ๋จผ์ € ํ”„๋กœ์ ํŠธ ํด๋”๋กœ ์ด๋™ ํ›„ ์•ฑ ์ƒ์„ฑ $ cd ํ”„๋กœ์ ํŠธ๋ช… $ python manage.py startapp ์•ฑ์ด๋ฆ„ 6. sett..

728x90