From b1aea97fc39191a410382644b4bf8d8625145e9b Mon Sep 17 00:00:00 2001 From: lauren Date: Tue, 27 Oct 2020 09:01:04 -0400 Subject: [PATCH] display tags --- .../migrations/0006_auto_20201027_1227.py | 23 +++++++++++++++++++ .../migrations/0007_auto_20201027_1250.py | 18 +++++++++++++++ studyguides/apps/courses/models.py | 4 ++-- studyguides/apps/courses/urls.py | 1 + studyguides/apps/courses/views.py | 8 +++++-- studyguides/templates/course.html | 4 ++-- studyguides/templates/tag.html | 14 +++++++++++ 7 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 studyguides/apps/courses/migrations/0006_auto_20201027_1227.py create mode 100644 studyguides/apps/courses/migrations/0007_auto_20201027_1250.py create mode 100644 studyguides/templates/tag.html diff --git a/studyguides/apps/courses/migrations/0006_auto_20201027_1227.py b/studyguides/apps/courses/migrations/0006_auto_20201027_1227.py new file mode 100644 index 0000000..1184349 --- /dev/null +++ b/studyguides/apps/courses/migrations/0006_auto_20201027_1227.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.6 on 2020-10-27 12:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('courses', '0005_auto_20201020_1737'), + ] + + operations = [ + migrations.AlterField( + model_name='guide', + name='tags', + field=models.ManyToManyField(to='courses.Tag'), + ), + migrations.AlterField( + model_name='subject', + name='courses', + field=models.ManyToManyField(related_name='subject', to='courses.Course'), + ), + ] diff --git a/studyguides/apps/courses/migrations/0007_auto_20201027_1250.py b/studyguides/apps/courses/migrations/0007_auto_20201027_1250.py new file mode 100644 index 0000000..272a3d6 --- /dev/null +++ b/studyguides/apps/courses/migrations/0007_auto_20201027_1250.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.6 on 2020-10-27 12:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('courses', '0006_auto_20201027_1227'), + ] + + operations = [ + migrations.AlterField( + model_name='guide', + name='tags', + field=models.ManyToManyField(related_name='guide', to='courses.Tag'), + ), + ] diff --git a/studyguides/apps/courses/models.py b/studyguides/apps/courses/models.py index d96fdfb..777f1d9 100644 --- a/studyguides/apps/courses/models.py +++ b/studyguides/apps/courses/models.py @@ -8,7 +8,7 @@ class Subject(models.Model): url = models.CharField("URL", max_length=20, unique=True, validators=[RegexValidator( regex="^[a-zA-Z0-9_\-]+$", message="Only alphanumeric, dashes, and underscores allowed")]) - courses = models.ManyToManyField("Course", related_name="subject", null=True, blank=True) + courses = models.ManyToManyField("Course", related_name="subject") def __str__(self): return self.name @@ -28,7 +28,7 @@ class Guide(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=100) - tags = models.ManyToManyField("Tag", null=True) + tags = models.ManyToManyField("Tag", related_name="guide") course = models.ForeignKey(Course, on_delete=models.CASCADE, null=True) url = models.URLField(max_length=300) diff --git a/studyguides/apps/courses/urls.py b/studyguides/apps/courses/urls.py index 5218b3c..78b6085 100644 --- a/studyguides/apps/courses/urls.py +++ b/studyguides/apps/courses/urls.py @@ -6,5 +6,6 @@ app_name = "courses" urlpatterns = [ path("/", views.subject_view), + path("tag//", views.tag_view, name="tag"), path("//", views.course_view), ] diff --git a/studyguides/apps/courses/views.py b/studyguides/apps/courses/views.py index 38e2d24..dea3d06 100644 --- a/studyguides/apps/courses/views.py +++ b/studyguides/apps/courses/views.py @@ -3,7 +3,7 @@ import random from django import http from django.shortcuts import render, redirect, reverse, get_object_or_404 -from .models import Subject, Course, Guide +from .models import Subject, Course, Guide, Tag def subject_view(request, subject_url): @@ -17,4 +17,8 @@ def course_view(request, subject_url, course_url): course = get_object_or_404(Course, url=course_url) return render(request, "course.html", {"subject": subject, "course": course, - "guides": Guide.objects.filter(course=course)}) + "guides": [[g, g.tags.all()] for g in Guide.objects.filter(course=course)]}) + +def tag_view(request, tag): + tag = get_object_or_404(Tag, name=tag) + return render(request, "tag.html", {"tag": tag, "guides": [[g, g.tags.all()] for g in tag.guide.all()]}) \ No newline at end of file diff --git a/studyguides/templates/course.html b/studyguides/templates/course.html index 5dec8c5..391308c 100644 --- a/studyguides/templates/course.html +++ b/studyguides/templates/course.html @@ -1,8 +1,8 @@ {% extends "base.html" %} {% load static %} {% block main %}

All / {{subject.name}} / {{course.name}}

- {% for guide in guides %} -

{% if guide.teacher %}[{{guide.teacher}}] {% endif %}{{ guide.name }}

+ {% for guide, tags in guides %} +

{{ guide.name }}    {% for t in tags %}{{ t }} {% endfor %}

{% endfor %}
{% endblock %} diff --git a/studyguides/templates/tag.html b/studyguides/templates/tag.html new file mode 100644 index 0000000..fe8beac --- /dev/null +++ b/studyguides/templates/tag.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} + +{% load static %} + +{% block main %} +
+

All / {{ tag }}

+ {% for guide, tags in guides %} +

+ {{ guide.name }}    {% for t in tags %}{{ t }} {% endfor %} +

+ {% endfor %} +
+{% endblock %} \ No newline at end of file