0

Im attempting to display all the course Names in my Course Model,

            {% for course in object_list%}
            <li>{{ course.courseName}} </li>
            {% endfor %}
            </ol>

Heres a snippet from the HTML page Im trying to display them in.

here's my models.py


# Create your models here.


SEMESTER_CHOICES = (
    ("SPR 21", "SPR 21"),
    ("FA 21", "FA 21"),
    ("SPR 22", "SPR 22"),
    ("FA 22", "FA 22"),
    ("SPR 23", "SPR 23"),
    ("FA 23", "FA 23"),
    ("SPR 24 ", "SPR 24"),
    ("FA 24", "FA 24"),
)
PROGRAM_CHOICES = (
    ("Architectural Science","Architectural Science"),
    ("Civil Engineering","Civil Engineering"),
    ("Computer Information Technology","Computer Information Technology"),
    ("Computer Science","Computer Science"),
    ("Construction Management","Construction Management"),
    ("Electrical Engineering","Electrical Engineering"),
    ("Engineering Technology Management","Engineering Technology Management"),
    ("Manufacturing Engineering","Manufacturing Engineering"),
    ("Mechanical_Engineering","Mechanical_Engineering")
)
# declaring a Student Model



class AddCourse(models.Model):

    program = models.CharField(
        max_length = 20,
        choices = PROGRAM_CHOICES,
        default = 'Architecural Science'
        )

    courseName = models.CharField(max_length=250)

    semester = models.CharField(
        max_length = 20,
        choices = SEMESTER_CHOICES,
        default = 'SPR 21'
        )

    preRequisites = models.TextField()


    def __str__ (self):
        return self.courseName

and here is my views.py

from django.shortcuts import render
from .models import AddCourse
from django.contrib.auth.decorators import login_required

# Create your views here.
@login_required

def currentpathway(request):

    return render(request, "SEAS_Course_Planner/home.html")
@login_required
def newpathway(request):
    return render(request, "SEAS_Course_Planner/newpathway.html")

Nothing is printing out and I believe the object list is empty, but I've added courses via the admin page, any suggestions. Im new to Django as you can tell hahaha.

3
  • 1
    can you post your views too ? Commented Dec 9, 2020 at 4:42
  • 1
    {% for course in object_list%} must be {% for course in object_list %} - the empty space before % is important, isn't it? Commented Dec 9, 2020 at 4:44
  • okay I added the views.py, and also the space - its still not displaying them. Commented Dec 9, 2020 at 4:44

1 Answer 1

2

I think you need to go through the docs from beginning.

Here in your view first you need to return the queryset from your Course model and need to pass the queryset as a context in your template.

def currentpathway(request):
    courses_list = AddCourse.objects.all()
    context = {'courses_list':courses_list}
    return render(request, "SEAS_Course_Planner/home.html", context)

Now in the template:

{% for course in courses_list %}
      <li>{{ course.courseName}} </li>
 {% endfor %}
Sign up to request clarification or add additional context in comments.

5 Comments

Okay, I went ahead and added that to the views, and also to the template, im still not seeing the courses however, any other ideas?
which is the view that renders your template( template where you are trying to show the courses)? @NightBeezy
its actually the newpathway not current pathway, but I went ahead and added it there. @arjun
assuming i'm understanding your question correctly, im attempting to display it on my newpathway.html file @arjun
"bash: syntax error near unexpected token `AddCourse.objects.all'" @arjun, I received this syntax error when I put it in the console, im not entirely for sure why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.