Converted def to class on views

parent ffaa93d0
from django.urls import path from django.urls import path
from .views import AssignmentsView, AssignmentCreateView, detail
from . import views
urlpatterns = [ urlpatterns = [
path('', views.index, name="assignment-list"), path('', AssignmentsView.as_view(), name="assignment-list"),
path('<int:pk>/details', views.detail, name="assignment-detail"), path('<int:pk>/details', detail, name="assignment-detail"),
path('add/', views.newassignment, name="add-assignment"), path('add/', AssignmentCreateView.as_view(), name="add-assignment"),
] ]
app_name = "assignments" app_name = "assignments"
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.http import HttpResponse from django.http import HttpResponse
from django.views import View
from django.views.generic.edit import CreateView
from .models import Assignment, Course from .models import Assignment, Course
from .forms import AssignmentForm from .forms import AssignmentForm
# Create your views here. class AssignmentsView(View):
def index(request): def get(self, request):
course_list = Course.objects.order_by("course_code") course_list = Course.objects.order_by("course_code")
assignments_list = Assignment.objects.order_by("name") assignments_list = Assignment.objects.order_by("name")
context = { return render(request, "assignments/assignments_list.html", {"course_list": course_list, "assignments_list": assignments_list})
"course_list": course_list,
"assignments_list": assignments_list, # def index(request):
} # course_list = Course.objects.order_by("course_code")
return render(request, "assignments/assignments_list.html", context) # assignments_list = Assignment.objects.order_by("name")
# context = {
# "course_list": course_list,
# "assignments_list": assignments_list,
# }
# return render(request, "assignments/assignments_list.html", context)
def detail(request, pk): def detail(request, pk):
assignment = Assignment.objects.get(pk=pk) assignment = Assignment.objects.get(pk=pk)
...@@ -23,12 +29,21 @@ def detail(request, pk): ...@@ -23,12 +29,21 @@ def detail(request, pk):
} }
return render(request, "assignments/assignments_detail.html", context) return render(request, "assignments/assignments_detail.html", context)
def newassignment(request): class AssignmentCreateView(CreateView):
if request.method == "POST": model = Assignment
assignment_form = AssignmentForm(request.POST) fields = '__all__'
if assignment_form.is_valid(): success_url = '/assignments/'
new_assignment = assignment_form.save()
def form_valid(request,form):
if form.is_valid():
new_assignment = form.save()
return redirect("assignments:add-assignment") return redirect("assignments:add-assignment")
else: # def newassignment(request):
assignment_form = AssignmentForm() # if request.method == "POST":
return render(request, "assignments/add_assignment.html", {"assignment_form": assignment_form}) # assignment_form = AssignmentForm(request.POST)
# if assignment_form.is_valid():
# new_assignment = assignment_form.save()
# return redirect("assignments:add-assignment")
# else:
# assignment_form = AssignmentForm()
# return render(request, "assignments/add_assignment.html", {"assignment_form": assignment_form})
...@@ -5,7 +5,7 @@ h1 { ...@@ -5,7 +5,7 @@ h1 {
text-align: left; text-align: left;
} }
form { label {
color: rgb(48, 0, 159); color: rgb(48, 0, 159);
font-family: 'Futura'; font-family: 'Futura';
font-size: 24px; font-size: 24px;
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<form action="{%url 'assignments:add-assignment' %}" method="POST"> <form action="{%url 'assignments:add-assignment' %}" method="POST">
{% csrf_token %} {% csrf_token %}
{{ assignment_form.as_p }} {{ form.as_p }}
<button class="Save Assignment" type="submit">Save Assignment</button> <button class="Save Assignment" type="submit">Save Assignment</button>
</form> </form>
......
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