Converted def to class on views

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