[Python] ์ฅ๊ณ shell/ํ ํ๋ฆฟ์ผ๋ก html ๋ถ๋ฌ์ค๊ธฐ/ํ ํ๋ฆฟ์ ์ ๋ณด ์ฒด์ฐ๊ธฐ/MVC ํจํด/์ฌ๋ก ์กฐ์ฌ ๋ชจ๋ธ
์ฅ๊ณ shell
๐ SHELL - ์ง์ DB์ ์์ฒญ์ ํ๋ ๋ค๋ฅธ๋ฐฉ๋ฒ
์ด์์ฒด์ ์ ๋ฐ๋ผ Shell ์คํ ๋ฐฉ๋ฒ
- windows - powershell์์
- unix/linux - ํฐ๋ฏธ๋์์
- manage.py ๊ฐ ์๋ ํด๋๋ก ์ด๋ ํ
- python manage.py shell
DB์ ์์ฒญํ๊ธฐ
- ๊ธฐ๋ณธ - ์ฌ์ฉํ ๋ชจ๋ธ ํด๋์ค import
>>> from elections.models import Candidate
- ๋ชจ๋ ๊ฐ์ฒด ๋ถ๋ฌ์ค๊ธฐ - all()
>>> Candidate.objects.all()
- ์ ๊ฐ์ฒด ์์ฑํ๊ณ ์ ์ฅํ๊ธฐ - Model.save()
>>> new_candidate = Candidate(name = "๋ฃจ๋น์ค") # ์์ฑ๋ง ํ ์ํ์ด๋ค. ์์ง ์ ์ฅ๋์ง ์์์.
>>> new_candidate.save() # new_candidate๊ฐ DB์ ์ ์ฅ๋จ
- ํน์ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ๊ฐ์ฒด ๋ถ๋ฌ์ค๊ธฐ - filter()
>>> no1 = Candidate.objects.filter(party_number = 1) # party_number = 1์ธ ๊ฐ์ฒด๋ฅผ ๋ฆฌ์คํธ ํํ๋ก return
>>> no1[0].party_number # ๋ฆฌ์คํธ ํํ์ด๊ธฐ ๋๋ฌธ์ index๋ก ์ ๊ทผ
>>> no1[0].name
Script ์ ์ฒด
ํ ํ๋ฆฟ์ผ๋ก html ๋ถ๋ฌ์ค๊ธฐ
- ์๋ฒ ์คํ - python manage.py runserver
- ๋ธ๋ผ์ฐ์ ์์ localhost:8000 ์ผ๋ก ์ ์
- HTML ์์ค๋ณด๊ธฐ
- windows
- internet Explore :
- ๋ง์ฐ์ค ์ฐํด๋ฆญ - ์์ค๋ณด๊ธฐ
- Ctrl + U
- Chrome :
- ๋ง์ฐ์ค ์ฐํด๋ฆญ - ํ์ด์ง ์์ค๋ณด๊ธฐ
- ์๋จ ๋ฉ๋ด๋ฐ์ ๋ณด๊ธฐ - ์์ค ๋๋
- Ctrl + U
- Firefox :
- ๋ง์ฐ์ค ์ฐํด๋ฆญ - ํ์ด์ง ์์ค
- Ctrl + U
- internet Explore :
- OSX
- Safari : [Safari - ํ๊ฒฝ์ค์ - ๊ณ ๊ธ - ํ๋จ์ ๋ฉ๋ด ๋ง๋์์ ๊ฐ๋ฐ์์ฉ ๋ฉ๋ด ๋ณด๊ธฐ ์ฒดํฌ] ํ์
- ๋ง์ฐ์ค ์ฐํด๋ฆญ - ํ์ด์ง ์์ค๋ณด๊ธฐ
- Command + Option + I
- Chrome :
- ๋ง์ฐ์ค ์ฐํด๋ฆญ - ํ์ด์ง ์์ค๋ณด๊ธฐ
- ์๋จ ๋ฉ๋ด๋ฐ์ ๋ณด๊ธฐ - ๊ฐ๋ฐ์ ์ ๋ณด - ์์ค๋ณด๊ธฐ
- Command + Option + U
- Firefox :
- ๋ง์ฐ์ค ์ฐํด๋ฆญ - ํ์ด์ง ์์ค๋ณด๊ธฐ
- Command + U
- Safari : [Safari - ํ๊ฒฝ์ค์ - ๊ณ ๊ธ - ํ๋จ์ ๋ฉ๋ด ๋ง๋์์ ๊ฐ๋ฐ์์ฉ ๋ฉ๋ด ๋ณด๊ธฐ ์ฒดํฌ] ํ์
- windows
- ํ
ํ๋ฆฟ ์ถ๊ฐํ๊ธฐ
- ์ฑ (elecetions) ํด๋ ์๋์ templates ํด๋ ์์ฑ (C\Code\mysite\elections\templates)
- templates ํด๋ ์๋ elections ํด๋ ์์ฑ (C\Code\mysite\elections\templates\elections\index.html)
- elections ํด๋ ์๋ index.html ํ์ผ ์์ฑ (C\Code\mysite\elections\templates\elections\index.html)
- index.html ๊ณผ views.py ์์
<!-- C\Code\mysite\elections\templates\elections\index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>์ ๊ฑฐ ํ๋ณด</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<td><B>์ด๋ฆ</B></td>
<td><B>์๊ฐ</B></td>
<td><B>์ถ๋ง์ง์ญ</B></td>
<td><B>๊ธฐํธ</B></td>
</tr>
</thead>
<tbody>
<tr>
<td>๊ฐํ๋ณด</td>
<td>ํ๋ณด์
๋๋ค.</td>
<td>์ฐ๋ฆฌ๋๋ผ</td>
<td>๊ธฐํธ1๋ฒ</td>
</tr>
<tr>
<td>๋ํ๋ณด</td>
<td>ํ๋ณด์
๋๋ค.</td>
<td>์ฐ๋ฆฌ๋๋ผ</td>
<td>๊ธฐํธ2๋ฒ</td>
</tr>
<tbody>
</table>
</body>
# C\code\mysite\elections\views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Candidate #models ์ ์ ์๋ Candidate๋ฅผ import
# Create your views here.
def index(request):
candidates = Candidate.objects.all()
return render(request, 'elections/index.html')
์๋ก๊ณ ์นจ ๊ฒฐ๊ณผ
ํ ํ๋ฆฟ์ ์ ๋ณด ์ฑ์ฐ๊ธฐ
์์ ๋ด์ฉ์ํ๋ณด์ ๋ํ ์ ๋ณด๋ฅผ ์ ์ ์ผ๋ก ๋ณด์ฌ์คฌ๋ค. ์ด๋ฒ์๋ DB์ ์๋ ์ ๋ณด๋ฅผ html html๋ก ์ ๋ฌํด ํ๋ณด์ ๋ํ ์ ๋ณด๋ฅผ ๋์ ์ผ๋ก ๋ณด์ด๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํ๋ค.
- views.py ์์ DB์ ์๋ ํ๋ณด ์ ๋ณด๋ฅผ html์ ์ ๋ฌ
# mysite\elections\views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import Candidate #models ์ ์ ์๋ Candidate๋ฅผ import
# Create your views here.
def index(request):
candidates = Candidate.objects.all()
context = {'candidates':candidates} #context์ ๋ชจ๋ ํ๋ณด์ ๋ํ ์ ๋ณด๋ฅผ ์ ์ฅ
return render(request, 'elections/index.html', context) # context๋ก html ์ ๋ชจ๋ ํ๋ณด์ ๋ํ ์ ๋ณด๋ฅผ ์ ๋ฌ
- index.html์์๋ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉฐ ์ ๋ฌ๋ฐ์ ํ๋ณด ์ ๋ณด๋ฅผ ๋ณด์ฌ์ค๋ค.
<!-- mysite\elections\templates\elections\index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>์ ๊ฑฐ ํ๋ณด</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<table class="table table-striped">
<thead>
<tr>
<td><B>์ด๋ฆ</B></td>
<td><B>์๊ฐ</B></td>
<td><B>์ถ๋ง์ง์ญ</B></td>
<td><B>๊ธฐํธ</B></td>
</tr>
</thead>
<!-- ์ด ๋ถ๋ถ์ด ์์ ๋จ -->
<tbody>
{% for candidate in candidates %}
<tr>
<td>{{candidate.name}}</td>
<td>{{candidate.introduction}}</td>
<td>{{candidate.area}}</td>
<td>๊ธฐํธ{{candidate.party_number}}๋ฒ</td>
</tr>
{% endfor %}
<!-- ์ฌ๊ธฐ๊น์ง -->
<tbody>
</table>
</body>
MVC ํจํด (Model View Controller Pattern)
๐ ์กฐ์จ, ๋ฐ์ดํฐ, ํ๋ฉด์ผ๋ก ํ๋ก๊ทธ๋จ์ ๊ตฌ์ฑํ๋ ๋ฐฉ์
- Model(๋ฐ์ดํฐ) - models.py
- candidate ํด๋์ค์ ํ์๋๋ก ๋ฐ์ดํฐ๋ฅผ DB์ ์ ์ฅ, ๋ถ๋ฌ์ด
- View(ํ๋ฉด) - templates
- ํ๋ฉด์ ์ด๋ค ์ฅ๋ฉด์ ๋ณด์ฌ์ค์ง๋ฅผ ๊ฒฐ์
- Controller(์กฐ์จ) - views.py
- Candidate ๋ชจ๋ธ์์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด, index.html์ ์ ๋ฌ
์ฌ๋ก ์กฐ์ฌ ๋ชจ๋ธ
๐ models.py ์๋ ๋ชจ๋ธ ํด๋์ค๋ฅผ ์ฌ๋ฌ ๊ฐ ์ ์ํ ์ ์๊ณ , ๋ชจ๋ธ ๊ฐ์ ๊ด๊ณ๋ฅผ ๋ํ๋ผ ์ ์๋ค.
- ์๋ก์ด ๋ชจ๋ธ์ models.py์ ์ ์ ํ๋ค.
#C:\mysite\elections\models.py
from django.db import models
# Create your models here.
class Candidate(models.Model):
name = models.CharField(max_length=10)
introduction = models.TextField()
area = models.CharField(max_length=15)
party_number = models.IntegerField(default=0)
def __str__(self):
return self.name
class Poll(models.Model):
start_date = models.DateTimeField()
end_date = models.DateTimeField()
area = models.CharField(max_length=15)
class Choice(models.Model):
poll = models.ForeignKey(Poll)
candidate = models.ForeignKey(Candidate)
votes = models.IntegerField(default=0)
- admin ์์ Poll ๋ชจ๋ธ์ ์ฌ์ฉํ๊ธฐ ์ํด, admin.py์ Poll์ ๋ฑ๋ก(regist)ํ๋ค.
from django.contrib import admin
from .models import Candidate, Poll
# Register your models here.
admin.site.register(Candidate)
admin.site.register(Poll)
- ๋ชจ๋ธ ๋ฑ๋ก
migration ํ์ผ์ ๋ง๋ ํ - migration ํ์ผ์ DB์ ๋ฐ์ - runserver ๊ณผ์ ์ ๋ฐ๋๋ค.
powershell ๋ฑ์์ manage.py ๊ฐ ์๋ ๋๋ ํ ๋ฆฌ๋ก ์ด๋ ํ ์๋ ๊ณผ์ ์ ์งํํ๋ค.
python manage.py makemigrations
- ๋ค์๊ณผ ๊ฐ์ด migrations ์ ํ์ผ์ด ๋ง๋ค์ด ์ก๋์ง ํ์ธํ๋ค.
- ์ Poll ํ๋์ id ํ๋๊ฐ ์๋์ผ๋ก ์์ฑ๋์์์ ์ ์ ์๋ค.
python manage.py migrate
python manage.py runserver
- ๋ธ๋ผ์ฐ์ ์์ localhost:8000/admin ์ผ๋ก ์ ์ํ๋ฉด Poll์ ๋ฑ๋กํ ์ ์๋ค.
๐ก ์์์ ์ฌ์ฉ๋ ํ๋ ํด๋์ค๋ฅผ ๊ฐ๋ตํ ์ค๋ช ํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
DateTimeField
date(๋ ์ง)์ time(์๊ฐ)์ ๋ํ๋ด๋ฉฐ, ptyhon์ datetime.datetime ์ธ์คํด์ค๋ก ํํ๋๋ค.
CharField
String์ ๋ํ๋ด๋ฉฐ, ํ์ ์ธ์ max_length๊ฐ ์๋ค.
๊ธธ์ด๊ฐ ๊ธด ๋ฌธ์์ด์ ์ ์ฅํ๋ ค๋ฉด TextField ๋ฑ์ ์ฐ๋ฉด ๋๋ค.
-CharField.max_lengt: ํด๋น ํ๋์ ์ต๋๊ธธ์ด๋ฅผ ์ค์ ํ๋ค. ์๋ฅผ ๋ค์ด 5๋ก ์ค์ ํ๋ฉด 5๊ธ์ ์ดํ๋ก๋ง ์ ์ฅํ ์ ์๋ค.
IntegerField
์ ์๋ฅผ ๋ํ๋ธ๋ค. ์ด ํ๋๋ Django๊ฐ ์ง์ํ๋ ๋ชจ๋ ๋ฐ์ดํฐ ๋ฒ ์ด์ค์์ -2147483648์์ 2147483647 ๊น์ง์ ์ ์๋ฅผ ์์ ํ๊ฒ ์ ์ฅํ๋ค. ๋ ํฐ ์ ์๋ BigIntegerField ๋ฑ์ ์ด์ฉํ๋ค.
ForeignKey
ํ ๋ชจ๋ธ์์ ๋ค๋ฅธ ๋ชจ๋ธ์ ์ด์ฉํ ๋์ ์ฐ์ธ๋ค.(๋ณดํต many-to-one ๊ด๊ณ ๋ชจ๋ธ์์ ์ด์ฉํ๋ค.)์์์์๋ ์ฌ๋ก ์กฐ์ฌ(Poll) id ํ๋์ ์ฌ๋ฌ ๊ฐ์ [ํ๋ณด-๋ํ์] ๋ฅผ ์ ์ฅํ๋ค.
์๋ก๊ณ ์นจ ๊ฒฐ๊ณผ
Poll object Add ํ ์๋ฃ