I have these 2 models

from django.db import models
from django.contrib.auth.models import User
from django.db.models import CharField

# Create your models here.

class Courses(models.Model):
    name = CharField(max_length=50)
    
    def __str__(self):
        return f'{self.name}'
    
class CoursesContext(models.Model):
    CoursesID = models.ForeignKey(Courses, on_delete=models.CASCADE)
    index = CharField(max_length=80)
    
    def __str__(self):
        return f'{self.index}'

I have this view that prints a list of courses.name

class CourseListView(ListView):
    model = Courses
    template_name = 'home/home.html'
    context_object_name= 'courses'

and this home.html

{% extends "home/base.html" %}
{% block content %}
     <h1>home screen</h1>
          br>
{% if user.is_authenticated %}
{% for course in courses %}          <!-- here python coding  -->
<article class="media content-section">
<div class="media-body">
    <div class="article-metadata">
      <a class="mr-2" href="#>{{ course.name }}</a> <!-- into access 
variable -->
    </div>
  </div>
</article>
{% endfor %} 
{% endif %} <!-- endfor or endif  -->
{% endblock content %}


the home.html at browser 



which is the way when i click a link prints his own index from CoursesContext 
model

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/726a4d09-0413-4656-bd8f-bbc829ac0883o%40googlegroups.com.

Reply via email to