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, DetailView, UpdateView, DeleteView
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 TeacherDetailView(DetailView):
# return only one model entry pk
# model_detail.html
model = Teacher
# PK -> {{teacher}}
class TeacherUpdateView(UpdateView):
model = Teacher
# fields = ['last_name', 'first_name']
fields = '__all__'
success_url = reverse_lazy('classroom:list_teacher')
class TeacherDeleteView(DeleteView):
# Form -> Confirm Delete Button
# default template name:
# model_confirm_delete.html
model = Teacher
success_url = reverse_lazy('classroom:list_teacher')
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, TeacherDetailView,
TeacherUpdateView, TeacherDeleteView)
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'),
path('teacher_detail/<int:pk>', TeacherDetailView.as_view(), name = 'detail_teacher'),
path('update_teacher/<int:pk>', TeacherUpdateView.as_view(), name = 'update_teacher'),
path('delete_teacher/<int:pk>', TeacherDeleteView.as_view())
]
# 세번째 파일
school > classroom > templates > classroom > teacher_confirm_delete.html
<h1>Are you sure? you want to delete this teacher?</h1>
<h2>{{teacher}}</h2>
<form method='POST'>
{% csrf_token %}
<input type='submit' value='Confirm Delete'>
</form>
# 네번째 파일
school > classroom > templates > classroom > teacher_list.html
<h1>List of Teacher (ListView)</h1>
<ul>
{% for teacher in teacher_list %}
<li>
<a href="/classroom/teacher_detail/{{teacher.id}}">{{teacher.first_name}} {{teacher.last_name}}</a>
</li>
<ul>
<li>
<a href='/classroom/update_teacher/{{teacher.id}}'>Update Information for {{teacher.first_name}}</a>
</li>
</ul>
<ul>
<li>
<a href='/classroom/delete_teacher/{{teacher.id}}'>DELETE{{teacher.first_name}}</a>
</li>
</ul>
{% endfor %}
</ul>
300x250
'개발일지 > Django' 카테고리의 다른 글
*매우중요 Django - 딕셔너리(dict)로 함수 간결화 하여서 views 호출하기 (0) | 2022.10.07 |
---|---|
*매우중요 Django - views파일에서 하나의 함수로 다중 urls 호출하기 (0) | 2022.10.07 |
Django - cbvUpdateView (0) | 2022.10.01 |
Django - cbvDetailView (0) | 2022.10.01 |
Django - cbv ListView (0) | 2022.10.01 |