Commit eea7aedf authored by Joan Denise Nocos's avatar Joan Denise Nocos

feat: converted index view from function-based (FBV) to class-based (CBV)

parent c2f3028e
from django.urls import path from django.urls import path
from . import views from . import views
from .views import IndexView
app_name = "assignments" app_name = "assignments"
urlpatterns = [ urlpatterns = [
# assignments/ # assignments/
path('', views.index, name='index'), path('', IndexView.as_view(), name='index'),
# assignments/<assignment_id>/details/ # assignments/<assignment_id>/details/
path('<int:assignment_id>/details/', views.details, name='details'), path('<int:assignment_id>/details/', views.details, name='details'),
# assignments/add/ # assignments/add/
......
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.shortcuts import render from django.shortcuts import render
from django.views import View
from assignments.models import Assignment, Course from assignments.models import Assignment, Course
from assignments.forms import AssignmentForm from assignments.forms import AssignmentForm
# Create your views here. # Create your views here.
def index(request): class IndexView(View):
course_list = Course.objects.order_by("course_code") def get(self, request):
assignment_list = Assignment.objects.all() course_list = Course.objects.order_by("course_code")
context = { assignment_list = Assignment.objects.all()
"course_list" : course_list, context = {
"assignment_list" : assignment_list, "course_list" : course_list,
} "assignment_list" : assignment_list,
return render(request, "assignments/index.html", context) }
return render(request, "assignments/index.html", context)
def details(request, assignment_id): def details(request, assignment_id):
try: try:
...@@ -37,8 +39,21 @@ def newassignment(request): ...@@ -37,8 +39,21 @@ def newassignment(request):
else: else:
assignment_form = AssignmentForm() assignment_form = AssignmentForm()
return render(request, "assignments/newassignment.html", {"assignment_form":assignment_form}) return render(request, "assignments/newassignment.html", {"assignment_form":assignment_form})
# Code from previous labs:
# Code from Lab 3:
#
# def index(request):
# course_list = Course.objects.order_by("course_code")
# assignment_list = Assignment.objects.all()
# context = {
# "course_list" : course_list,
# "assignment_list" : assignment_list,
# }
# return render(request, "assignments/index.html", context)
# Code from Midterm Project and earlier labs:
# #
# def index(request): # def index(request):
# assignments_view = "ASSIGNMENTS: <br>" # assignments_view = "ASSIGNMENTS: <br>"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment