본문 바로가기

Development (Python, Django, C..)

[Django] Tutorial 따라하기 - 3

* Reference : https://docs.djangoproject.com/ko/2.1/intro/tutorial03/

 

첫 번째 장고 앱 작성하기, part 3 | Django 문서 | Django

Django The web framework for perfectionists with deadlines. Overview Download Documentation News Community Code Issues About ♥ Donate

docs.djangoproject.com

 

#. Add View


-- add view to polls/views.py

(djenv) ivan@django:~/djgo$ more polls/views.py 
#from django.shortcuts import render 

from django.http import HttpResponse 


def index(request): 
    return HttpResponse("Hello, world. You're at the poll index") 

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 of question %s." 
    return HttpResponse(response % question_id) 

def vote(request, question_id): 
    return HttpResponse("You're voting on question %s." % question_id) 
(djenv) ivan@django:~/djgo$ 




-- add path to URLs

(djenv) ivan@django:~/djgo$ more polls/urls.py 
from django.urls import path 
from . import views 

urlpatterns = [ 
    # ex: /polls/ 
    path('', views.index, name='index'), 
    # ex: /polls/5/ 
    path('/', views.detail, name='detail'), 
    # ex: /polls/5/results/ 
    path('/results/', views.results, name='results'), 
    # ex: /polls/5/vote/ 
    path('/vote/', views.vote, name='vote'), 
] 




-- Using views

(djenv) ivan@django:~/djgo$ more polls/views.py 
from django.http import HttpResponse 

from .models     import Question 

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

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 of question %s." 
    return HttpResponse(response % question_id) 

def vote(request, question_id): 
    return HttpResponse("You're voting on question %s." % question_id) 
(djenv) ivan@django:~/djgo$ 




-- Using template

(djenv) ivan@django:~/djgo$ mkdir polls/templates 
(djenv) ivan@django:~/djgo$ mkdir polls/te 
templates/ tests.py 
(djenv) ivan@django:~/djgo$ mkdir polls/templates/polls 


(djenv) ivan@django:~/djgo$ more polls/templates/polls/index.html 
{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

 


-- Update index view

(djenv) ivan@django:~/djgo$ more polls/views.py 
from django.http      import HttpResponse 
#from django.shortcuts import render 
from django.template  import loader 

from .models          import Question 

def index(request): 
    latest_question_list = Question.objects.order_by('-pub_date')[:5] 
    template = loader.get_template('polls/ndex.html') 
    context = {'latest_question_list': latest_question_list} 
    return HttpResponse(template.render(context,request)) 

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 of question %s." 
    return HttpResponse(response % question_id) 

def vote(request, question_id): 
    return HttpResponse("You're voting on question %s." % question_id) 
(djenv) ivan@django:~/djgo$ 




#. Error 404


-- ID apply  

(djenv) ivan@django:~/djgo$ more polls/views.py 
from django.http      import HttpResponse 
from django.shortcuts import render 
#from django.template  import loader 

from .models          import Question 

def index(request): 
    latest_question_list = Question.objects.order_by('-pub_date')[:5] 
    template = loader.get_template('polls/index.html') 
    context = {'latest_question_list': latest_question_list} 
    return HttpResponse(template.render(context,request)) 

def detail(request, question_id): 
    try: 
        question = Question.objecjts.get(pk=question_id) 
    except Question.DoesNotExist: 
        raise Http404("Question does not exist") 
    return render(request, 'polls/detail.html', {'question': question}) 


def vote(request, question_id): 
    return HttpResponse("You're voting on question %s." % question_id) 
(djenv) ivan@django:~/djgo$ 





-- shotcut : get_object+or+404()

(djenv) ivan@django:~/djgo$ more polls/views.py 
from django.http      import Http404 
from django.http      import HttpResponse 
from django.shortcuts import render, get_object_or_404 
from django.template  import loader 

from .models          import Question 

def index(request): 
    latest_question_list = Question.objects.order_by('-pub_date')[:5] 
    #template = loader.get_template('polls/index.html') 
    context = {'latest_question_list': latest_question_list} 
    return render(request, 'polls/index.html', context) 

def detail(request, question_id): 
    #try: 
    #    question = Question.objects.get(pk=question_id) 
    #except Question.DoesNotExist: 
    #    raise Http404("Question does not exist") 
    #return render(request, 'polls/detail.html', {'question': question}) 
    question = get_object_or_404(Question, pk=question_id) 
    return render(request, 'polls/detail.html', {'question': question}) 


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

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