사용자 도구

사이트 도구


python:django:view

뷰(view)

views.py

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)

polls/views.py

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)

template

프로젝트의 TEMPLATES 설정은 Django가 어떻게 템플릿을 불러오고 렌더링 할 것인지 기술합니다. 기본 설정 파일은 APP_DIRS 옵션이 True로 설정된 DjangoTemplates 백엔드를 구성합니다. 관례에 따라, DjangoTemplates은 각 INSTALLED_APPS 디렉토리의 《templates》 하위 디렉토리를 탐색합니다.

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 %}
python/django/view.txt · 마지막으로 수정됨: 2025/04/15 10:05 저자 127.0.0.1