Commit 2a6291c9 authored by Joan Denise Nocos's avatar Joan Denise Nocos

feat: added ModelForm for Assignment model

parent 65a57193
from django.forms import ModelForm
from .models import Assignment, Course
class AssignmentForm(ModelForm):
class Meta:
model = Assignment
fields = ["name", "description", "max_points", "course", "image"]
\ No newline at end of file
{% extends "assignments/base.html" %}
{% block page-title %}New Assignment{% endblock %}
{% block content %}
<h1>New Assignment</h1>
<form method="POST" action="{% url 'assignments:newassignment' %}">
{% csrf_token %}
{{ assignment_form.as_p }}
<button class="button" type="submit">Save Assignment</button>
</form>
{% endblock %}
\ No newline at end of file
from django.http import HttpResponse, Http404 from django.http import HttpResponse, Http404
from django.shortcuts import render from django.shortcuts import render
from assignments.models import Assignment, Course from assignments.models import Assignment, Course
from assignments.forms import AssignmentForm
# Create your views here. # Create your views here.
def index(request): def index(request):
...@@ -28,7 +29,14 @@ def details(request, assignment_id): ...@@ -28,7 +29,14 @@ def details(request, assignment_id):
return render(request, "assignments/details.html", context) return render(request, "assignments/details.html", context)
def newassignment(request): def newassignment(request):
return HttpResponse("New Assignment Page") if request.method == "POST":
assignment_form = AssignmentForm(request.POST)
if assignment_form.is_valid():
new_assignment = assignment_form.save()
return redirect("assignments:newassignment")
else:
assignment_form = AssignmentForm()
return render(request, "assignments/newassignment.html", {"assignment_form":assignment_form})
# Code from previous labs: # Code from previous labs:
# #
......
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