사용자 도구

사이트 도구


python:django:view

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
다음 판
이전 판
python:django:view [2021/10/04 03:07] – [뷰(view)] taekgupython:django:view [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1
줄 1: 줄 1:
 +====== 뷰(view) ======
 +===== views.py =====
 +
 +<code python views.py>
 +from django.shortcuts import render
 +from django.views import generic
 +# Create your views here.
 +
 +class index(generic.ListView):
 +    def __init__(self):
 +        self.title_nm       = "메인페이지입니다."
 +        self.ogImgUrl       = ""
 +        self.descript       = "메인페이지입니다."
 +        self.template_name  = "blog/index.html"
 +
 +    def get(self, request, *args, **kwargs):        
 +        self.content = {"descript":self.descript,
 +                        "title_nm":self.title_nm,
 +                        "ogImgUrl":self.ogImgUrl,                       
 +                        "dataList":"[[[[ Hellow DJango ]]]]"}
 +
 +        return render(request, self.template_name, self.content)
 +</code>
 +===== polls/views.py =====
 +
 +<code python polls/views.py>
 +
 +def index(request):
 +    # return HttpResponse("Hello, world! You're at the polls index.")
 +    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)
 +</code>
 +
 +===== template =====
 +프로젝트의 TEMPLATES 설정은 Django가 어떻게 템플릿을 불러오고 렌더링 할 것인지 기술합니다. 기본 설정 파일은 APP_DIRS 옵션이 True로 설정된 DjangoTemplates 백엔드를 구성합니다. 관례에 따라, DjangoTemplates은 각 INSTALLED_APPS 디렉토리의 《templates》 하위 디렉토리를 탐색합니다.
 +
 +<code python 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 %}
 +</code>