본문 바로가기
개발일지/Django

Django - cbv ListView

by 다니엘의 개발 이야기 2022. 10. 1.
320x100

# 첫번째 파일

school > classroom > views.py

from django.shortcuts import render
# 이것은 내가 찾고 있는 URL의 실제 이름을 리턴한다.
# reverse는 string타입을 리턴하고 reverse_lazy는 object 타입을 리턴한다.
# 또한 success_url 을 사용하는경우, reverse_lazy를 사용한다.
from django.urls import reverse, reverse_lazy
from django.views.generic import TemplateView, FormView, CreateView, ListView
from classroom.models import Teacher
from classroom.forms import ContactForm

# Create your views here.
# def home_view(request):
#     return render(request, 'classroom/home.html')

class HomeView(TemplateView):
    template_name = 'classroom/home.html'

class ThankYouView(TemplateView):
    template_name = 'classroom/thank_you.html'

class TeacherCreateView(CreateView):
    model = Teacher
    # model_form.html
    # .save()
    fields = '__all__'
    success_url = reverse_lazy('classroom:thank_you')

class TeacherListView(ListView):
    # model_list.html
    model = Teacher
    # queryset = Teacher.objects.all()
    queryset = Teacher.objects.order_by('first_name')

    context_object_name = 'teacher_list'
    

class ContactFormView(FormView):
    form_class = ContactForm
    template_name = 'classroom/contact.html'

    # URL NOT a template.html
    success_url = reverse_lazy('classroom:thank_you')

    # what to do with form?
    def form_valid(self, form):
        print(form.cleaned_data)

        # ContactForm(request.POST)
        return super().form_valid(form)

# 두번째 파일

school > classroom > urls.py

from django.urls import path
from .views import (HomeView, ThankYouView, 
                    ContactFormView, TeacherCreateView,
                    TeacherListView)

app_name = 'classroom'

# domain.com/classroom/
urlpatterns = [
    path('', HomeView.as_view(), name = 'home'), # path expects a function
    path('thank_you/', ThankYouView.as_view(), name = 'thank_you'), # path expects a function
    path('contact/', ContactFormView.as_view(), name = 'contact'),
    path('create_teacher/', TeacherCreateView.as_view(), name = 'create_teacher'),
    path('list_teacher/', TeacherListView.as_view(), name = 'list_teacher')
]

# 세번째 파일

school > classroom > templates > classroom > home.html

<h1>welcome to home.html</h1>
<li>
    <ul>
        <a href='{% url 'classroom:thank_you' %}'>Thank you page link</a>
    </ul>
    <ul>
        <a href='{% url 'classroom:contact' %}'>Contact form page link</a>
    </ul>
    <ul>
        <a href='{% url 'classroom:create_teacher' %}'>Create New Teacher page link</a>
    </ul>
    <ul>
        <a href='{% url 'classroom:list_teacher' %}'>List Teacher page link</a>
    </ul>


</li>

# 네번째 파일

school > classroom > templates > classroom > teacher_list.html

<h1>List of Teacher (ListView)</h1>
<ul>
    {% for teacher in teacher_list %}
        <li>{{teacher.first_name}} {{teacher.last_name}}</li>
    {% endfor %}
</ul>
300x250

'개발일지 > Django' 카테고리의 다른 글

Django - cbvUpdateView  (0) 2022.10.01
Django - cbvDetailView  (0) 2022.10.01
Django - cbv CreateView  (0) 2022.10.01
Django - cbv formview  (0) 2022.10.01
Django - cbv template view  (0) 2022.10.01