added courses to assignments app, linked course model to assignment model,...

added courses to assignments app, linked course model to assignment model, added assignment.html template
parent 48ec8443
from django.contrib import admin from django.contrib import admin
# Register your models here. # Register your models here.
from .models import Assignment from .models import Assignment, Course
admin.site.register(Assignment) admin.site.register(Assignment)
admin.site.register(Course)
# Generated by Django 4.0.3 on 2022-04-04 16:53
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('assignments', '0002_alter_assignment_description_alter_assignment_name'),
]
operations = [
migrations.CreateModel(
name='Course',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('course_code', models.CharField(max_length=10)),
('course_title', models.CharField(max_length=100)),
('section', models.CharField(max_length=3)),
],
),
migrations.AddField(
model_name='assignment',
name='passing_score',
field=models.IntegerField(blank=True, editable=False, null=True),
),
migrations.AddField(
model_name='assignment',
name='course',
field=models.ForeignKey(default='1', on_delete=django.db.models.deletion.CASCADE, to='assignments.course'),
),
]
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class Course(models.Model):
course_code = models.CharField(max_length=10)
course_title = models.CharField(max_length=100)
section = models.CharField(max_length=3)
def __str__(self):
return self.course_title
class Assignment(models.Model): class Assignment(models.Model):
course = models.ForeignKey(Course, on_delete=models.CASCADE,default="1")
name = models.CharField(max_length=100) name = models.CharField(max_length=100)
description = models.CharField(max_length=500) description = models.CharField(max_length=500)
max_points = models.IntegerField() max_points = models.IntegerField()
passing_score = models.IntegerField(blank=True, null=True, editable=False)
def __str__(self): def __str__(self):
return self.name return self.name
def save(self, *args, **kwargs):
self.passing_score = int(self.max_points * 0.6)
super().save(*args, **kwargs)
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Assignments:</h1>
{% for assignment in assignments %}
Assignment Name: {{assignment.name}} <br>
Description: {{assignment.description}} <br>
Perfect Score: {{assignment.max_points}} <br>
Passing Score: {{assignment.passing_score}} <br>
Course/Section: {{assignment.course.course_code}} | {{assignment.course.course_title}} | {{assignment.course.section}}
<br><br>
{% endfor %}
</body>
</html>
from django.http import HttpResponse from django.http import HttpResponse
from . models import Assignment, Course
from django.shortcuts import render
# Create your views here. # Create your views here.
def index(request): def index(request):
return HttpResponse("This is the Assignments page!")
context ={
'assignments':Assignment.objects.all(),
'courses':Course.objects.all()
}
return render(request, "assignment.html", context)
No preview for this file type
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