Django View & URL (2)

์žฅ๊ณ ๋Š” URLconf(URL configuration)์„ ์‚ฌ์šฉํ•œ๋‹ค. URLconf ๋Š” URL ํŒจํ„ด์„ views์— ์—ฐ๊ฒฐํ•œ๋‹ค. ์ด์ „์— Django View ์™€ URL (1)์—์„œ url์„ ์ž ๊น ๋‹ค๋ฃฌ์ ์ด ์žˆ๋‹ค. ์ด ์žฅ์—์„œ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณผ ๊ฒƒ์ด๋‹ค.

View์™€ URL

views.py

# app/views.py
from django.http import HttpResponse


def index(request):
    return HttpResponse("<h1>Hello, world</h1>")

def detail(request, question_id):
    return HttpResponse("You're looking at question %s" % question_id)

def results(request, question_id):
    response = "You're looking at the results %s"
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You're voting on question %s" % question_id)

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
    path('<int:question_id>/',views.detail, name='detail'),
    path('<int:question_id>/results/',views.results, name='results'),
    path('<int:question_id>/vote/',views.vote, name='vote'),
]

๋‹ค์Œ๊ณผ ๊ฐ™์ด view์™€ url์„ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

http://localhost:8000/polls, http://localhost:8000/polls/1, http://localhost:8000/polls/1/results, http://localhost:8000/polls/1/vote ์™€ ๊ฐ™์€ url๋กœ view์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์‚ฌ์šฉ์ž๊ฐ€ ์›น์‚ฌ์ดํŠธ์˜ ํŽ˜์ด์ง€๋ฅผ ์š”์ฒญํ•˜๋ฉด Django๋Š” mysite.urls(project.urls) ํŒŒ์ด์ฌ ๋ชจ๋“ˆ์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. ๊ฑฐ๊ธฐ์„œ urlpatterns ๋ณ€์ˆ˜๋ฅผ ์ฐพ์•„ ์ˆœ์„œ๋Œ€๋กœ ํŒจํ„ด์„ ๋”ฐ๋ผ๊ฐ„๋‹ค. ๊ทธ url์— ๋งž๋Š” viewํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ๋œ๋‹ค.

show_urls

ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์— ์„ค์ •๋˜์–ด์žˆ๋Š” URL์„ ํ•œ๋ฒˆ์— ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ์šฐ์„  django-extensions ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์ค€๋‹ค. [์„ค์น˜ํ•˜๊ธฐ]

$ pip install django-extensions
$ pip list
Package             Version
------------------- -------
Django              2.1.7
django-extensions   2.1.6
djangorestframework 3.9.2
pip                 19.0.3
pytz                2018.9
setuptools          40.6.2
six                 1.12.0

๊ทธ๋ฆฌ๊ณ  ๋‚œํ›„ settings.py์— ์„ค์ •ํ•ด์ค€๋‹ค.

INSTALLED_APPS = (
    'django_extensions',
)
$ ./manage.py
[django_extensions]
    admin_generator
    clean_pyc
    clear_cache
    compile_pyc
    create_command
    create_jobs
    create_template_tags
    delete_squashed_migrations
    describe_form
    drop_test_database
    dumpscript
    export_emails
    find_template
    generate_password
    generate_secret_key
    graph_models
    mail_debug
    merge_model_instances
    notes
    passwd
    pipchecker
    print_settings
    print_user_for_session
    reset_db
    reset_schema
    runjob
    runjobs
    runprofileserver
    runscript
    runserver_plus
    set_default_site
    set_fake_emails
    set_fake_passwords
    shell_plus
    show_template_tags
    show_templatetags
    show_urls
    sqlcreate
    sqldiff
    sqldsn
    sync_s3
    syncdata
    unreferenced_files
    update_permissions
    validate_templates

์—ฌ๊ธฐ์„œ url์„ ํ™•์ธํ•˜๋ ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด๋œ๋‹ค.

$ ./manage.py show_urls
/    rest_framework.routers.APIRootView    api-root
/\.<format>/    rest_framework.routers.APIRootView    api-root
/api-auth/login/    django.contrib.auth.views.LoginView    rest_framework:login
/api-auth/logout/    django.contrib.auth.views.LogoutView    rest_framework:logout
/groups/    quickstart.views.GroupViewSet    group-list
/groups/<pk>/    quickstart.views.GroupViewSet    group-detail
/groups/<pk>\.<format>/    quickstart.views.GroupViewSet    group-detail
/groups\.<format>/    quickstart.views.GroupViewSet    group-list
/users/    quickstart.views.UserViewSet    user-list
/users/<pk>/    quickstart.views.UserViewSet    user-detail
/users/<pk>\.<format>/    quickstart.views.UserViewSet    user-detail
/users\.<format>/    quickstart.views.UserViewSet    user-list

View

๊ฐ View๋Š” 1. ์š”์ฒญ๋œ ํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์ด ๋‹ด๊ธด HttpResponse ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜, 2. ์˜ˆ์™ธ์ฒ˜๋ฆฌ(Http404)๋ฅผ ํ•˜๋„๋ก ๋˜์–ด์žˆ๋‹ค.

  1. ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค ๋ ˆ์ฝ”๋“œ ์ฝ๊ธฐ

  2. ํ…œํ”Œ๋ฆฟ ์‹œ์Šคํ…œ ์‚ฌ์šฉ

  3. PDF ์ƒ์„ฑ

  4. XML ์ถœ๋ ฅ

  5. ์‹ค์‹œ๊ฐ„ ZIP ํŒŒ์ผ ์ƒ์„ฑ

view๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ผ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, python์˜ ์–ด๋– ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

Database API ์‚ฌ์šฉํ•˜๊ธฐ

from django.http import HttpResponse
# ๋ชจ๋ธ import
from .models import Question

def index(request):
    last_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ', '.join([q.question_text for q in last_question_list])
    return HttpResponse(output)

Database API์— ์ ‘๊ทผํ•˜๊ณ  ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. View์—์„œ ํŽ˜์ด์ง€์˜ ๋””์ž์ธ์„ ํ•˜๋“œ์ฝ”๋”ฉ ํ•˜๊ณ  ์žˆ๋Š” ์ ์ด๋‹ค. ํ…œํ”Œ๋ฆฟ(template)์„ ์ด์šฉํ•ด์„œ python์ฝ”๋“œ๋กœ ๋ถ€ํ„ฐ ๋””์ž์ธ์„ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฐธ์กฐ

Last updated