Django, AWS

์›ยท2022๋…„ 9์›” 27์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
16/46
post-thumbnail

์•„์นจ ์ชฝ์ง€์‹œํ—˜

๐ŸŸก models.py์— AccessLog ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ

  • created_at : ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ๋œ ์‹œ๊ฐ„์„ ์•Œ ์ˆ˜ ์žˆ๋„๋ก datetimefield ์‚ฌ์šฉ
  • location : ์‚ฌ์šฉ์ž๊ฐ€ ์–ด๋–ค template์— ์ ‘๊ทผํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋„๋ก charfield ์‚ฌ์šฉ

๐ŸŸก ์‚ฌ์šฉ์ž๊ฐ€ introduce ํŽ˜์ด์ง€์— ์ ‘์†ํ–ˆ์„ ๋•Œ, ์ ‘์† ๋กœ๊ทธ๋ฅผ ๋‚จ๊ธฐ๊ธฐ

  • admin ํŽ˜์ด์ง€์—์„œ ๋กœ๊ทธ๋ฅผ ๋ดค์„ ๋•Œ ์–ธ์ œ ์–ด๋–ค ํŽ˜์ด์ง€์— ์ ‘์†ํ–ˆ๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค
  • ๋กœ๊ทธ๋ฅผ ์ €์žฅํ•  ๋•Œ๋Š” ์–ด๋–ค ํƒฌํ”Œ๋ฆฟ์„ ์กฐํšŒํ–ˆ๋Š”์ง€๋„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผํ•œ๋‹ค

๋‹ต์•ˆ
admin.py

from django.contrib import admin
from .models import AccessLog 

admin.site.register(AccessLog)

models.py

from django.db import models
class AccessLog(models.Model):
    """
    1. default : ๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ๋  ๋‚ ์งœ๋ฅผ ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •
    2. auto_now : ๋ฐ์ดํ„ฐ๊ฐ€ ์ˆ˜์ • ๋  ๋•Œ๋งˆ๋‹ค ๊ฐฑ์‹ ๋จ
    3. auto_now_add : ๋ฐ์ดํ„ฐ๊ฐ€ ์ƒ์„ฑ ๋  ๋•Œ ์‹œ๊ฐ„์„ ๊ธฐ๋ก
    """
    created_at = models.DateTimeField("์ ‘์† ์‹œ๊ฐ„", auto_now_add=True)
    location = models.CharField("์ ‘์† ๊ฒฝ๋กœ", max_length=50)
    
    def __str__(self):
        return f"{self.created_at} / {self.location}"

views.py

from django.shortcuts import render
from .models import AccessLog

def introduce(request):
    # case 1
    """
    access_log = AccessLog()
    access_log.location = "introduce"
    access_log.save()
    """
    # case 2
    AccessLog.objects.create(
        location="introduce"
    )
    
    return render(request, 'introduce.html')

djangi taggit ์„ค์น˜

์žฅ๊ณ  ํƒœ๊ทธ๋ชจ๋“ˆ ๋‹ค์šด

pip install django-taggit
pip install django-taggit-templatetags2

๐Ÿ‘‰ ๋‹ค์šด๋กœ๋“œ ํ›„ ํ”„๋กœ์ ํŠธ settings์— ์ถ”๊ฐ€


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'tweet',
    'user',
    'restaurant',
    'taggit.apps.TaggitAppConfig',
    'taggit_templatetags2',
]

TAGGIT_CASE_INSENSITIVE = True
TAGGIT_LIMIT = 50

์™„์„ฑํ•œ ํ”„๋กœ์ ํŠธ github์— ์˜ฌ๋ฆฌ๊ธฐ

1) db.sqlite3 ์ง€์šฐ๊ธฐ
2) ๊ฐ ํด๋”์˜ migrations ํด๋”์— inti.py๋งŒ ๋‚จ๊ธฐ๊ณ  ๋‹ค ์ง€์šฐ๊ธฐ
3) ํ”„๋กœ์ ํŠธ settings.py ์ˆ˜์ •

DEBUG = False

ALLOWED_HOSTS = ["*"]

4) ๋ ˆํฌ์ง€ํ† ๋ฆฌ ์ƒ์„ฑ
5) ์ถ”๊ฐ€

git init

git add .

git commit -m "first commit"

git branch -M main 

git remote add origin https://github.com/paullee714/mySpartaSns.git

git push -u origin main

AWS ์„œ๋ฒ„์— ๋‚ด ํ”„๋กœ์ ํŠธ ์˜ฌ๋ฆฌ๊ธฐ

1) AWS์—์„œ EC2 ์ƒ์„ฑ ๋ฐ port ์„ค์ •
EC2 ์„œ๋น„์Šค ์ ‘๊ทผ ํ›„ '์ธ์Šคํ„ด์Šค' ํด๋ฆญ
์ธ์Šคํ„ด์Šค ์‹œ์ž‘

  • Ubuntu Server 20.04 LTS
  • ๋ณด์•ˆ ๊ทธ๋ฃน ์ถ”๊ฐ€ ๋ฐ ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •


    2) ํ‚ค ํŽ˜์–ด ์ƒ์„ฑ ๋ฐ ๋‹ค์šด๋กœ๋“œ

    3) AWS์—์„œ git, python 3 ์„ค์น˜ํ•˜๊ธฐ
  • ์ธ์Šคํ„ด์Šค ์—ฐ๊ฒฐ
  • ํ„ฐ๋ฏธ๋„ ์ฐฝ์ด ์—ด๋ฆฌ๋ฉด
sudo apt-get update
  • git repository ์ถ”๊ฐ€
git clone https://github.com/paullee714/mySpartaSns.git

cd mySpartaSns

sudo apt-get install python3-pip -y

pip3 install django django-taggit django-taggit-templatetags2

python3 manage.py makemigrations

python3 manage.py migrate

python3 manage.py runserver 0.0.0.0:8000

4) IPv4 ์ฃผ์†Œ ๋’ค์— :8000 ๋ถ™์ด๊ณ  ์ ‘์†. ์„œ๋ฒ„ ์—ฐ๊ฒฐ ํ™•์ธ
5) ํฌํŠธ๋ฒˆํ˜ธ ์—†์• ๊ธฐ

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8000

nohup์œผ๋กœ ์žฅ๊ณ  ์‹คํ–‰ํ•˜๊ธฐ

nohup python3 manage.py runserver 0.0.0.0:8000 &

nohup ๋„๋Š” ๋ฒ•

ps -ef | grep -i 'manage.py runserver'

kill 20212

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