feat: allow creation of assignment entry from assignment index page

Create add.html file in template, required view and url path.
Edit index.html to add button with link to page to allow addition of new entry.

Delete static images folder and allow images to be uploaded instead.
parent 738063c2
from statistics import mode
from django.forms import ModelForm
from .models import Assignment
class AssignmentForm(ModelForm):
class Meta:
model = Assignment
fields = ["name", "description", "max_points", "course", "image"]
\ No newline at end of file
# Generated by Django 3.2.12 on 2022-05-26 11:19
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('assignments', '0002_auto_20220330_2257'),
]
operations = [
migrations.AddField(
model_name='assignment',
name='image',
field=models.ImageField(blank=True, null=True, upload_to='images/'),
),
]
......@@ -14,7 +14,7 @@ class Course(models.Model):
)
def __str__(self):
return self.course_code + " - " + self.course_title
return self.course_code + " - " + self.course_title +" | " + self.section
class Assignment(models.Model):
name = models.CharField(max_length=50)
......@@ -26,6 +26,11 @@ class Assignment(models.Model):
default=None,
null=True
)
image = models.ImageField(
null=True,
blank=True,
upload_to="images/"
)
def __str__(self):
return self.name
......
{% extends "assignments/base.html" %}
{% block content %}
<h1>Add Assignment</h1>
<form method="POST" enctype="multipart/form-data" action="{% url 'assignments:add' %}">
{% csrf_token %}
{{assignment_form.media}}
{{ assignment_form.as_p }}
<button class="button" type="submit">Save Assignment</button>
</form>
<br>
<button type="button"> <a href="{% url 'assignments:index' %}" id = "back"> Back to Main Assignments Page</a> </button>
{% endblock %}
\ No newline at end of file
......@@ -9,12 +9,8 @@
{{assignment.max_points}} <br>
{{assignment.passing_score}} <br>
{% if assignment.id == 1 %}
<img src = "{% static 'assignments/images/history.jpg' %}" alt = "history">
{% elif assignment.id == 2 %}
<img src = "{% static 'assignments/images/koreanlang.png' %}" alt = "korean speaking">
{% else %}
<img src = "{% static 'assignments/images/yoga.jpg' %}" alt = "yoga">
{% if assignment.image %}
<img src="{{ assignment.image.url }}">
{% endif %}
......
......@@ -19,8 +19,7 @@
{% endfor %}
</p>
<button type="button"> <a href="{% url 'assignments:add' %}" id="new"> New Assignment</a> </button>
{% endblock %}
......
......@@ -6,6 +6,8 @@ urlpatterns = [
path('', views.index, name = 'index'),
#assignments/1/details
path("<int:assignment_id>/details/", views.detail, name='detail'),
#assignments/add
path("add/", views.add, name='add'),
]
app_name = "assignments"
\ No newline at end of file
......@@ -2,6 +2,8 @@ from unicodedata import name
from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect
from .models import Course, Assignment
from .forms import AssignmentForm
# Create your views here.
......@@ -18,3 +20,15 @@ def detail(request, assignment_id):
except Assignment.DoesNotExist:
raise Http404("Assignment does not exist!")
return render(request, "assignments/detail.html", {"assignment": assignment})
def add(request):
if request.method == "POST":
assignment_form = AssignmentForm(request.POST, request.FILES)
if assignment_form.is_valid():
new_assignment = assignment_form.save()
return redirect("assignments:add")
else:
assignment_form = AssignmentForm()
context = {"assignment_form": assignment_form}
return render(request, "assignments/add.html", context)
\ No newline at end of file
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