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()

생성을 μœ„ν•΄μ„œλŠ” μ•„λž˜μ™€ 같이도 ν•  수 μžˆλ‹€!

Article.objects.create(title='제λͺ©', content='λ‚΄μš©')
  1. 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">
    <div data-gb-custom-block data-tag="csrf_token"></div>

</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="

<div data-gb-custom-block data-tag="url" data-0='articles:index'></div>"> ... </a>

<a href="<div data-gb-custom-block data-tag="url" data-0='articles:delete'></div>"> ... </a>

<form class="form-inline" action="<div data-gb-custom-block data-tag="url" data-0='articles:search'></div>" 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