#๊ฐ์ํ๊ฒฝ ๋ง๋ค๊ธฐ
conda create -n "๊ฐ์ํ๊ฒฝ์ด๋ฆ" python=3.7
#๊ฐ์ํ๊ฒฝ ๋ฆฌ์คํธ ํ์ธ
conda env list
# ๊ฐ์ํ๊ฒฝ ํ์ฑํํ๊ธฐ
conda activate "๊ฐ์ํ๊ฒฝ์ด๋ฆ"
# ๊ฐ์ํ๊ฒฝ ๋นํ์ฑํํ๊ธฐ
conda deactivate
#๊ฐ์ํ๊ฒฝ ์ญ์ ํ๊ธฐ
conda env remove -n "๊ฐ์ํ๊ฒฝ์ด๋ฆ"
# ์ฅ๊ณ ์ค์น ํ์ธ
python -m django version
#mysite๋ผ๋ ํ๋ก์ ํธ ๋ง๋ค๊ธฐ
django-admin startproject mysite
#์๋ฒ ๋๋ฆฌ๊ธฐ (mysite ์์น์์ ์คํ)
python manage.py runserver
#์ฑ ๋ง๋ค๊ธฐ
python manage.py startapp polls
- ์ฑ์ ํ๋ก์ ํธ ๋ด๋ถ์ ๊ฐ๋ณ์ ์ธ ์ดํ๋ฆฌ์ผ์ด์ ์ด๋ค. ์ฆ ํ๋ก์ ํธ ์คํ๋๋ ์ฌ๋ฌ ์๋น์ค ๊ธฐ๋ฅ๋ค์ ๋งํ๋ค. ex) polls, ์ด์ฉ๊ตฌ , ์ด์ฉ๊ตฌ
polls/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
views.py
#polls/views.py
from django.http import HttpResponse
#๊ธฐ๋ณธ์ ์ธ ํํ์ view
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
โก๏ธ view๋ฅผ ํธ์ถํ๋ ค๋ฉด ์ด์ ์ฐ๊ฒฐ๋ url์ด ํ์ํ๋ ์ด๋ URLconf๊ฐ ์ฌ์ฉ๋๋ค. polls์ URLconf๋ฅผ ์์ฑํ๊ธฐ ์ํด polls์์ urls.py ๋ง๋ ๋ค.
#mysite/urls.py
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
โก๏ธ ๊ทธ๋ฆฌ๊ณ ์ต์์ URLconfig์์ polls.urls๋ฅผ ๋ฐ๋ผ๋ณด๊ฒ ์ค์ ํ๊ธฐ ์ํด์ mysite/urls.py urlpatterns์ include() ํจ์๋ฅผ ์์ ๊ฐ์ด ์ถ๊ฐํ๋ค.
python manage.py runserver
โก๏ธ url์ด ์ ์ฐ๊ฒฐ๋๋์ง ํ์ธํ๊ธฐ ์ํด ์๋ฒ ์คํ
path์ธ์
#๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ
์ด๋ธ์ ๋ง๋ค๊ธฐ ์ํด ์คํ
python manage.py migrate
from django.db import models
#polls/models.py
# ๋ ๊ฐ์ง ํ๋๋ฅผ ๋ง๋ฌ - Question(์ง๋ฌธ ๋ด์ฉ,๋ฐํ์ผ)๊ณผ Choice(์ ํ์ง,๋ชํ)
# class : DB ํ
์ด๋ธ
# class์ variable : ์นผ๋ผ (DB filed)
# class์ object : Row (DB record)
#question_text์ pub_date๋ db ํ
์ด๋ธ์ ํ๋ ์ด๋ฆ
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
โก๏ธ vscode ์ค๋ช ์ถ๊ฐํ๊ธฐ
#mysite/settings.py
INSTALLED_APPS = [
'polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
โก๏ธ ์ฑ์ ํ์ฌ์ ํ๋ก์ ํธ์ ํฌํจ์ํค๊ธฐ ์ํด์ installed_apps์ polls์ ์๋ contribd์ ์๋ pollsconfig ํด๋์ค๋ฅผ ๋ฃ๋๋ค.
phthon manage.py makemigrations polls
โก๏ธ makemigration์ ์คํ์ํด์ผ๋ก์จ ๋ชจ๋ธ์ ๋ณ๊ฒฝ์ํจ ์ฌ์ค์ ์ฅ๊ณ ์๊ฒ ์๋ ค์ค๋ค.
#์์ ์คํ์ํค๊ธฐ
python manage.py shell
>>> from polls.models import Choice, Question
#model class ์ ํ์ง์ ์ง๋ฌธ importํ๊ธฐ
>>> Question.objects.all()
# qustion์ ๋ํ ๋ชจ๋ ๋ฐ์ดํฐ ๊ฐธ์ ธ์ค๊ธฐ
<QuerySet []>
# question ๋ด์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ธฐ - ์์ง์๋ ์ํ
# question๋ด์ ๋ฐ์ดํฐ ์์ฑํ๊ธฐ - ๋ชจ๋ธ์๋ ์ง๋ฌธ๊ณผ ๋ฐํ์ผ์ง ์กด์ฌํ์
>>> from django.utils import timezone #๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ
>>> q = Question(question_text="What's new?", pub_date=timezone.now())
# ๋ชจ๋ธ Question์ question_text์ "What's new" ๋ผ๋ ์ง๋ฌธ ์ถ๊ฐํด q์ ๋ฃ๊ธฐ
# ๋ฐ์ดํฐ ๋ฒ ์ด์ค์ ์ ! ์ฅ!
>>> q.save()
# Now it has an ID.
# id๋ ์ฐ๋ฆฌ๊ฐ ๋ช
์ํ์ง ์์๋ ์ฅ๊ณ ๊ฐ ์๋์ผ๋ก ๋ง๋ค์ด์ค.
>>> q.id
1
#์ด์ what's new๋ผ๋ ์ง๋ฌธ์ id๋ 1
# Access model field values via Python attributes.
# ๋ด๊ฐ ๋ฃ์ ํ
์คํธ ํ์ธ
>>> q.question_text
"What's new?"
#๋ด๊ฐ ๋ฃ์ ๋ฐํ์ผ ํ์
>>> q.pub_date
datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)
# ํ
์คํธ ์์ ๋ ๊ฐ๋ฅ ๋์ save()๋ฅผ ํด์ค์ผ ํจ.
>>> q.question_text = "What's up?"
>>> q.save() #์ ์ฅ
# objects.all() displays all the questions in the database.
>>> Question.objects.all()
<QuerySet [<Question: Question object (1)>]>
# ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ด์ ์๋ ์ง๋ฌธ์ด ๋ช๊ฐ์ธ์ง ์์ ์์. - ํ์ฌ๋ 1๊ฐ
โก๏ธ ํ์ง๋ง <Question: Question object (1)>์ผ๋ก๋ ๊ฐ์ฒด๋ฅผ ํํํ๋๋ฐ ๋ณ๋ก ๋์์ด ๋์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ์ปค์คํ ๋ฉ์๋ ์ถ๊ฐ ๋ฑ์ models.py๋ฅผ ์์ ํ ์์ ๋ค์ ์คํํ๊ธฐ
#polls/models.py
import datetime
from django.db import models
from django.utils import timezone
# ๋ ๊ฐ์ง ํ๋๋ฅผ ๋ง๋ฌ - Question(์ง๋ฌธ ๋ด์ฉ,๋ฐํ์ผ)๊ณผ Choice(์ ํ์ง,๋ชํ)
# class : DB ํ
์ด๋ธ
# class์ variable : ์นผ๋ผ (DB filed)
# class์ object : Row (DB record)
#question_text์ pub_date๋ db ํ
์ด๋ธ์ ํ๋ ์ด๋ฆ
class Question(models.Model): #DB ํ
์ด๋ธ for ์ค๋ฌธ์กฐ์ฌ ์ฃผ์ธ
question_text = models.CharField(max_length=200) # ์ค๋ฌธ์กฐ์ฌ ์ฃผ์ ํ
์คํธ(์ง๋ฌธ)
pub_date = models.DateTimeField('date published') # 'date published' ๊ด๋ฆฌ์ ํ์ด์ง์์ ๋ณด์ฌ์ง ํญ๋ชฉ๋ช
# ๋งจ ๋ฐ์ ์ค๋ช
๊ณผ ๋์ผ ์ฆ shell์ด๋ ๊ด๋ฆฌ์ ํ์ด์ง์์ DB ํ
์ด๋ธ ๋ด์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋์ ๋ ๋ณด์ฌ์ง๋ ํ
์คํธ๋ฅผ ์ง์
def __str__(self):
return self.question_text
# ์ปค์คํ
๋ฉ์๋ ์ถ๊ฐ :
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
# ํ์ฌ ๊ธฐ์ค์ผ๋ก ํ๋ฃจ ์ ์์ ๋ณด๋ค ๋ ์ดํ์ ๋ฑ๋ก๋ Question์ธ์ง ์ฌ๋ถ๋ฅผ ํ์ธํด์ค (true/fasle)
#ํ๋์ question์ ๋ค์์ choice๋ฅผ ๊ฐ์ง๊ธฐ ๋ผ๋ฌธ์ ์ผ๋๋ค ๊ตฌ์กฐ
class Choice(models.Model): #DB ํ
์ด๋ธ for ์ค๋ฌธ์กฐ์ฌ ์ฃผ์ ๋ณ ์ ํ์ง(+ ์ ํ์ง๋ง๋ค์ ๋ํ ์)
question = models.ForeignKey(Question, on_delete=models.CASCADE) #์ค๋ฌธ์กฐ์ฌ ์ฃผ์ ์ id ๊ฐ
# foreignkey? - ์์ ์์ฑ๋ Question์ด๋ผ๋ ๋ชจ๋ธ(ํ
์ด๋ธ)์ ์ฐธ์กฐํจ.
# CASCADE๋ ์์ Question์ ๊ฐ์ด ์ญ์ ๋ ๋ ์ด ์ธ๋ํค๋ฅผ ํฌํจํ๋ ๋ชจ๋ธ row(์ ํ์ง)๋ ์ญ์ ๋๋ค๋ ์๋ฏธ
choice_text = models.CharField(max_length=200) #์ค๋ฌธ์กฐ์ฌ ์ฃผ์ ์ ๋ํ ์ ํ์ง ํ
์คํธ
votes = models.IntegerField(default=0) # ํด๋น ์ ํ์ง์ ๋ํ ์
# ์์ <Question: Question object (1)> ์ ๋ฐฉ์์ ๋ณ ๋์์ด ์๋จ. ๋ฐ๋ผ์ ์ด ๋ฉ์๋๋ฅผ ์ถ๊ฐํด ์์์ ํธํ๊ฒ ์ฌ์ฉ๊ฐ๋ฅ.
def __str__(self):
return self.choice_text
โก๏ธ ์์์ ๊ฐ์ฒด์ ํํ์ ๋ ์์ธํ ๋ณด๊ฒํ ์ปค์คํ ๋ฉ์๋๋ str()๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ค.
#์์ ์คํ์ํค๊ธฐ
python manage.py shell
โก๏ธ ๋ค์ ์์ ์คํ ์์ผ์ ํ์ธํด๋ณด๊ธฐ
>>> from polls.models import Choice, Question
# Make sure our __str__() addition worked.
# question ๋ด์ ๋ชจ๋ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
>>> Question.objects.all()
<QuerySet [<Question: What's up?>]>
# models.py ๋ด์ฉ ์์ ์ผ๋ก ์ธํ str๋ฉ์๋์ ๋ํ ๋ด์ฉ ํ์ธ ๊ฐ๋ฅ # question_text์ ๋ณผ ์ ์์. ์๋์ <QuerySet [<Question: Question object (1)>]> ์ด๊ฑฐ ์์.
# Django provides a rich database lookup API that's entirely driven bykeyword arguments.
>>> Question.objects.filter(id=1) # id๊ฐ 1๋ฒ์ธ question์ ๊ฐ์ง๊ณ ์๋ผ
<QuerySet [<Question: What's up?>]>
>>>
Question.objects.filter(question_text__startswith='What') # ํ์ค์ฒ ํ
์คํธ๊ฐ what์ผ๋ก ์์ํ๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ผ
<QuerySet [<Question: What's up?>]>
# Get the question that was published this year.
# ์ฌํด ๋ฐํ๋ ์ง๋ฌธ
>>> from django.utils import timezone # ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ
>>> current_year = timezone.now().year #์ฌํด
>>> Question.objects.get(pub_date__year=current_year)
<Question: What's up?>
#๋ง์ฝ์ ์๋ Id๋ฅผ ์์ฒญํ๋ฉด, ์๋ฌ
>>> Question.objects.get(id=2)
Traceback (most recent call last):
...
DoesNotExist: Question matching query does not exist.
# Lookup by a primary key is the most common case, so Django provides a
# shortcut for primary-key exact lookups.
# ์ฆ ๋์ด ๊ฐ์.
# Question.objects.get(id=1) = Question.objects.get(pk=1)
>>> Question.objects.get(pk=1) # pk๊ฐ 1์ธ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
<Question: What's up?>
>>> q = Question.objects.get(pk=1) # pk๊ฐ 1์ธ ๋ฐ์ดํฐ๋ฅผ q์ ์ ์ฅ
>>> q.was_published_recently() # ์ปค์คํ
๋ฉ์๋๋ก ํ์ธ(์ต๊ทผ์ ์ ์ฅ๋์ด์๋์ง ํ์ธํ๋ ๋ฉ์๋,)
True #์ต๊ทผ์ ๋ง๋ค์ด์ง!
# Give the Question a couple of Choices. The create call constructs a new
# Choice object, does the INSERT statement, adds the choice to the set
# of available choices and returns the new Choice object. Django creates
# a set to hold the "other side" of a ForeignKey relation
# (e.g. a question's choice) which can be accessed via the API.
>>> q = Question.objects.get(pk=1) # pk๊ฐ 1์ธ question์ q์ ์ ์ฅ
# Display any choices from the related object set -- none so far.
#q์ ์ ํ์ง๋ค์ ๋ถ๋ฌ์ค๊ธฐ - ์์ง ์๋์ํ
>>> q.choice_set.all()
<QuerySet []>
# Create three choices. - 3๊ฐ์ง ์ด์ด์ค ๋ง๋ค๊ธฐ
>>> q.choice_set.create(choice_text='Not much', votes=0)
<Choice: Not much>
>>> q.choice_set.create(choice_text='The sky', votes=0)
<Choice: The sky>
>>> c = q.choice_set.create(choice_text='Just hacking again', votes=0)
# ๋ง๋ค์ด์ง Choice.object๋ question object์ ์ ๊ทผ ํ ์ ์๋ api๋ฅผ ๊ฐ์ง. ์ฆ ๋๋ต์ผ๋ก ์ง๋ฌธ ์ ๊ทผ ๊ฐ๋ฅ.
# Choice objects have API access to their related Question objects.
>>> c.question
<Question: What's up?>
# ๋ฐ๋๋ ๊ฐ๋ฅ- ์ง๋ฌธ์ผ๋ก ์ด์ด์ค ์ ๊ทผ ๊ฐ๋ฅ
>>> q.choice_set.all()
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
>>> q.choice_set.count() # ์ด์ด์ค ๋ช๊ฐ์ธ์ง ํ์ธ
3
# The API automatically follows relationships as far as you need.
# Use double underscores to separate relationships. # ๊ด๊ณ๋ฅผ ๋๋๋ ์ฌ์ฉ
# This works as many levels deep as you want; there's no limit.
# Find all Choices for any question whose pub_date is in this year
# (reusing the 'current_year' variable we created above).
# ์์ ์ฌ์ฉํ ๊ฒ current_year ์ ๋ค์ ๊ฐ์ ธ์์ ๋ฃ์ผ๋ฉด
#---
>>> from django.utils import timezone # ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ํฌํธ
>>> current_year = timezone.now().year
>>> Question.objects.get(pub_date__year=current_year)
<Question: What's up?>
#---
#choice๊ฐ ์ฌํด ๋ง๋ค์ด์ก๋์ง ํ์ธ
>>> Choice.objects.filter(question__pub_date__year=current_year) #์ฌํด ๋ง๋ค์ด์ง choice๋ง ๊ฐ์ ธ์ค๊ธฐ
<QuerySet [<Choice: Not much>, <Choice: The sky>, <Choice: Just hacking again>]>
# Let's delete one of the choices. Use delete() for that. ์ญ์ ํ๊ธฐ
>>> c = q.choice_set.filter(choice_text__startswith='Just hacking') #ํดํน์ ์ญ์
>>> c.delete()
python manage.py createsuperuser
#์ํ๋ ์ ์ ๋ค์๊ณผ ์ด๋ฉ์ผ , ํจ์ค์๋ ๋ฃ๊ธฐ
Username :
Email address :
#์๋ฒ ๋ค์ ์คํํ๊ณ http://127.0.0.1:8000/admin/์ผ๋ก ๋ค์ด๊ฐ๊ธฐ
python manage.py runserver
#polls/admin.py
from django.contrib import admin
from .models import Question
admin.site.register(Question)
โก๏ธ admin.py๋ฅผ ์์ ํ๋ฉด ๊ด๋ฆฌ ์ฌ์ดํธ์์ polls๋ผ๋ ์ฑ์ ์์ ํ ์ ์๋ค.