URL Name

1. DB

DB ์ดˆ๊ธฐํ™”

  1. db.sqlite3 ์‚ญ์ œ

  2. migration file ์‚ญ์ œ

migration file ํ™•์ธ

$ python manage.py showmigrations

๋‹ค์‹œ migrations ๋งŒ๋“ค๊ธฐ

$ python manage.py makemigrations

๋Œ€์‘๋˜๋Š” SQL๋ฌธ ์ถœ๋ ฅ

$ python manage.py sqlmigrate articles 0001
                            [app_label] [migration_name]
BEGIN;
--
-- Create model Article
--
CREATE TABLE "articles_article" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "title" varchar(140) NOT NULL, "content" text NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
COMMIT;

migrate ํ•˜๊ธฐ

$ python manage.py migrate
        [app_label] [migration_name]

create()

์ƒ์„ฑ์„ ์œ„ํ•ด์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด๋„ ํ•  ์ˆ˜ ์žˆ๋‹ค!

  1. Article.objects.create(title='์ œ๋ชฉ', content='๋‚ด์šฉ')
  2. fd

    article = Article(title='์ œ๋ชฉ', content='๋‚ด์šฉ')
    article.save()

IntegrityError

: This exception is raised when the relational integrity of the data is affected.

2. GET & POST method

  • GET

    • data๋ฅผ ๊ฐ€์ ธ์˜ค๋‹ค

    • ํŠน์ • ๋ฆฌ์†Œ์Šค์˜ ํ‘œ์‹œ

    • <a> tag <form> tag ๋ฐ ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฃผ์†Œ์ฐฝ์„ ๋ณด๋‚ด๋Š” ์š”์ฒญ ๋“ฑ

    • URL์„ ํ™œ์šฉ (querystring) ํ•˜์—ฌ data๋ฅผ ์ „์†กํ•จ

      • ํฌ๊ธฐ ์ œํ•œ & ๋ณด์•ˆ ์ด์Šˆ๊ฐ€ ์žˆ์Œ

  • POST

    • data๋ฅผ ๊ฒŒ์‹œํ•˜๋‹ค

    • ํŠน์ • ๋ฆฌ์†Œ์Šค์— ์ œ์ถœ (์„œ๋ฒ„์˜ ์ƒํƒœ ๋ณ€ํ™”)

    • ๋ณดํ†ต HTML Form์„ ํ†ตํ•ด ์„œ๋ฒ„์— ์ „์†กํ•˜๋ฉฐ, ์„œ๋ฒ„์˜ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ๋งŒ๋“ฆ

    • HTTP ์š”์ฒญ ๋ฉ”์‹œ์ง€์˜ body์— data๋ฅผ ์ „์†กํ•จ

HTTP (Hyper Text Markup Language)

  • Resource๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” protocol

  • ์›น์—์„œ ์ด๋ฃจ์–ด์ง€๋Š” ๋ชจ๋“  ๊ตํ™˜์˜ ๊ธฐ์ดˆ

Request

  • URL (Uniform Resource Locators)

    • Web์—์„œ ์ •ํ•ด์ง„ ์œ ์ผํ•œ ์ž์›์˜ ์ฃผ์†Œ

  • ํ”„๋กœํ† ์ฝœ :// ๋„๋ฉ”์ธ: ํฌํŠธ/ ๊ฒฝ๋กœ(path)/?ํŒŒ๋ผ๋ฏธํ„ฐ#์•ต์ปค

Response

views.py ์ˆ˜์ •

def create(request):
    article = Article()
    article.title= request.POST.get('title')
    article.content = request.POST.get('content')
    article.save()
    return redirect(f'/articles/{article.pk}/')

CSRF token ์ถ”๊ฐ€ํ•˜๊ธฐ

<form action="/articles/create/" method="POST">
    {% csrf_token %}
</form>

hidden ๊ฐ’์œผ๋กœ csrf token์ด ์ถ”๊ฐ€๋˜์–ด ์žˆ๋Š” ๊ฒƒ ํ™•์ธ ๊ฐ€๋Šฅ

Curl ๋กœ ๊ฐ„๋‹จํ•œ ์š”์ฒญ ๋‚ ๋ฆฌ๊ธฐ

$ curl -X GET http://chloecodes1.pythonanywhere.com/community/

Telnet ์‚ฌ์šฉํ•ด๋ณด๊ธฐ

์„ค์น˜

$ sudo apt-get install telnetd

request ๋‚ ๋ฆฌ๊ธฐ

$ telnet google.com 80

3. app_name ์ง€์ •ํ•˜๊ธฐ

urls.py

from django.urls import path
from . import views

app_name = 'articles'

urlpatterns = [
    # /articles/
    path('', views.index, name='index'),
    path('new/', views.new, name='new'),
    path('create/',views.create, name='create'),
    path('<int:pk>/',views.detail, name='detail'),
    path('delete/<int:pk>/',views.delete, name='delete'),
    path('edit/<int:pk>/', views.edit, name='edit'),
    path('update/<int:pk>/', views.update, name='update'),
]

views.py

from django.shortcuts import render, redirect, get_object_or_404
from .models import Article

# Create your views here.

def index(request):
    articles = Article.objects.all()
    context = {
        'articles': articles
    }

    return render(request, 'articles/index.html', context)

def new(request):
    return render(request, 'articles/new.html')

def create(request):
    article = Article()
    article.title= request.POST.get('title')
    article.content = request.POST.get('content')
    article.save()

    # return redirect(f'/articles/{article.pk}/')
    return redirect('articles:detail', article.pk)


def detail(request, pk):
    article = Article.objects.get(id=pk)
    context = {
        'article':article
    }
    return render(request,'articles/detail.html', context)


def delete (request, pk):
    article = Article.objects.get(id=pk)
    article.delete()

    # return redirect(f'/articles/')
    return redirect('articles:index')


def edit(request, pk):
    article = get_object_or_404(Article, id = pk)
    context = {
        'article': article
    }

    return render(request, 'articles/edit.html', context)

def update (request,pk):
    article = Article.objects.get(id=pk)
    article.title = request.POST.get('title')
    article.content = request.POST.get('content')
    article.save()

    return redirect(f'/articles/{article.pk}/')

In html

<a class="navbar-brand" href="{% url 'articles:index' %}"> ... </a>

<a href="{% url 'articles:delete' article.pk %}"> ... </a>

<form class="form-inline" action="{% url 'articles:search' %}" method="POST"> ... </form>

4. get_object_or_404

article = Article.objects.get(id=pk)

  • get( ) ์€ ๊ฐ’์ด ์—†์œผ๋ฉด error๋ฅผ ๋„์›€

    • ๋‹จ ํ•˜๋‚˜๋ฅผ returnํ•˜๋Š” method

  • ๊ทธ๋ž˜์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ

    from django.shortcuts import render, redirect, get_object_or_404
        ...
    article = get_object_or_404(Article, id=pk)

5. Static files

settings.py

# servering ๋˜๋Š” URL ์•ž์— ๋ถ™์Œ
STATIC_URL = '/static/'

# app directory ๊ฐ€ ์•„๋‹Œ static ํด๋” ์ง€์ •
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

+

Traceroute

TraceRoute - Linux / TRACERT - Windows

์ง€์ •๋œ ํ˜ธ์ŠคํŠธ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ†ต๊ณผํ•˜๋Š” ๊ฒฝ๋กœ์˜ ์ •๋ณด์™€ ๊ฐ ๊ฒฝ๋กœ์—์„œ์˜ ์ง€์—ฐ ์‹œ๊ฐ„์„ ์ถ”์ ํ•˜๋Š” ๋ช…๋ น์ด๋‹ค, ์‰ฝ๊ฒŒ ๊ฒฝ๋กœ ์ถ”์  ํˆด์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค (ICMP์„ ์‚ฌ์šฉํ•œ๋‹ค!)

  • ์ง€์ •๋œ ํ˜ธ์ŠคํŠธ์— ๋„๋‹ฌํ•  ๋•Œ๊นŒ์ง€ ํ†ต๊ณผํ•˜๋Š” ๊ฒฝ๋กœ์˜ ์ •๋ณด์™€ ๊ฐ ๊ฒฝ๋กœ์—์„œ์˜ ์ง€์—ฐ ์‹œ๊ฐ„์„ ์ถ”์ ํ•˜๋Š” ๋„คํŠธ์›Œํฌ ๋ช…๋ น์–ด๋กœ ํŠน์ • ์‚ฌ์ดํŠธ์— ์ ‘์†์ด ๋˜์ง€ ์•Š๊ฑฐ๋‚˜ ์ง€์—ฐ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ์–ด๋””์—์„œ ๋ณ‘๋ชฉ์ด ๋ฐœ์ƒํ•˜๋Š”์ง€๋ฅผ ์•Œ์•„๋ณด๋Š”๋ฐ ์œ ์šฉํ•จ.

  • ์ ‘์†์ด ๋˜๋Š” ๊ฐ ๊ฒฝ๋กœ๋ฅผ ์ฒดํฌํ•˜์—ฌ ์–ด๋Š ๊ฒฝ๋กœ(Routing)๋ฅผ ๊ฑฐ์ณ ์ ‘์†์ด ๋˜๊ณ , ์–ด๋Š ๊ตฌ๊ฐ„์—์„œ ์–ผ๋งˆ๋งŒํผ์˜ ์†๋„ ์ง€์—ฐ์ด ์žˆ๋Š”์ง€, ๊ทธ๋ฆฌ๊ณ  ์–ด๋””์—์„œ ํŒจํ‚ท์ด ์ค‘์ง€ ๋๋Š”์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ

  • ๋‹จ, ์‹œ๊ฐ„๋Œ€/๋‚ด๋ถ€ ํŠธ๋ž˜ํ”ฝ/์„œ๋ฒ„ ์ƒํƒœ ๋“ฑ์˜ ๋งŽ์€ ์˜ํ–ฅ์„ ๋ฐ›์•„ ๊ฐ’์ด ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ฐ˜๋ณต ํ™•์ธ์ด ํ•„์š”ํ•˜๋‹ค!

Install traceroute

$ sudo apt-get install traceroute

Use traceroute

$ traceroute www.google.com
traceroute to www.google.com (172.217.31.164), 30 hops max, 60 byte packets
 1  _gateway (172.30.1.254)  5.195 ms  5.127 ms  5.105 ms
 2  220.78.3.1 (220.78.3.1)  5.071 ms * *
 3  125.141.249.21 (125.141.249.21)  5.364 ms  5.308 ms  5.262 ms
 4  * * *
 5  * * *
 6  112.174.73.178 (112.174.73.178)  6.510 ms 112.174.47.162 (112.174.47.162)  5.301 ms 112.174.73.178 (112.174.73.178)  4.858 ms
 7  74.125.52.16 (74.125.52.16)  31.913 ms  31.801 ms  33.951 ms
 8  108.170.242.129 (108.170.242.129)  36.142 ms 108.170.242.97 (108.170.242.97)  34.386 ms  34.811 ms
 9  209.85.253.109 (209.85.253.109)  36.711 ms  36.555 ms  36.483 ms
10  nrt12s22-in-f4.1e100.net (172.217.31.164)  34.998 ms  33.267 ms  32.844 ms

MVC Pattern

  • model driven design

  • data modeling

Last updated