I thought I would quickly document how to access data passed from a Django view to a template. This wasn't completely clear to me until recently - and the power of Django is evident once you grasp how to access data values past from a QuerySet. The power here lies in the data model itself. For example - If you have a model which references foreign key - you have complete access all the way back through the data chain to get whatever you need. Its all accessed via a dot notation in the Template itself. For example if your model looks something like this:

CODE:
    
class ForumContributors(models.Model):
    username = models.CharField(_('username'), max_length=75, unique=True) 
    firstName = models.CharField(max_length=20)
    lastName = models.CharField(max_length=30)

class ForumThread(models.Model):
    heading = models.ForeignKey(ForumSections, on_delete=models.CASCADE)
    threadTitle = models.CharField(max_length=200)
    threadStatus = models.BooleanField(default=True)
    lastUpdate = models.DateTimeField(auto_now=True)

class Post(models.Model):
    contributor = models.ForeignKey(ForumContributors, on_delete=models.CASCADE)
    postText = models.TextField()
    value = models.IntegerField(default=0)
    thread = models.ForeignKey(ForumThread, on_delete=models.CASCADE)
    

In this case lets say you have run a query in your view that looks like this where ThreadID is a variable requested by the user:

CODE:
    
postData = Post.objects.filter(thread__pk=threadID)

context =  {
           ......
           ......
          'thisPost' : postData,
}
return render(request,'forum/postDetail.html', context)
    

postData now contains a complete wealth of information which, when passed to your template via the 'context' dictionary variable 'thisPost', can be accessed through multiple levels of depth. What do I mean? For example. Lets say you want to get the username of the person who posted a specific post. You can access this in the template by simply doing this in the template:

CODE:
    
{% for post in thisPost %}
#1script type="text/javascript"#2
       console.log("post from: {{ post.contributor.username }}");
 #1/script#2
    

Literally post contains not only the post information but any and all the information you want from the contributor foreign key by simple dot notation. Truly beautiful. I'm sure you all knew this already but for me it was a useful realization. Hope it helps somebody!!

Cheers!!

Tommy

Navigate

I thought I would quickly document how to access data passed from a Django view to a template. This wasn't completely clear to me until recently - and the power of Django is evident once you grasp how to access data values past from a QuerySet. The power here lies in the data model itself. For example - If you have a model which references foreign key - you have complete access all the way back through the data chain to get whatever you need. Its all accessed via a dot notation in the Template itself. For example if your model looks something like this:

CODE:
    
class ForumContributors(models.Model):
    username = models.CharField(_('username'), max_length=75, unique=True) 
    firstName = models.CharField(max_length=20)
    lastName = models.CharField(max_length=30)

class ForumThread(models.Model):
    heading = models.ForeignKey(ForumSections, on_delete=models.CASCADE)
    threadTitle = models.CharField(max_length=200)
    threadStatus = models.BooleanField(default=True)
    lastUpdate = models.DateTimeField(auto_now=True)

class Post(models.Model):
    contributor = models.ForeignKey(ForumContributors, on_delete=models.CASCADE)
    postText = models.TextField()
    value = models.IntegerField(default=0)
    thread = models.ForeignKey(ForumThread, on_delete=models.CASCADE)
    

In this case lets say you have run a query in your view that looks like this where ThreadID is a variable requested by the user:

CODE:
    
postData = Post.objects.filter(thread__pk=threadID)

context =  {
           ......
           ......
          'thisPost' : postData,
}
return render(request,'forum/postDetail.html', context)
    

postData now contains a complete wealth of information which, when passed to your template via the 'context' dictionary variable 'thisPost', can be accessed through multiple levels of depth. What do I mean? For example. Lets say you want to get the username of the person who posted a specific post. You can access this in the template by simply doing this in the template:

CODE:
    
{% for post in thisPost %}
#1script type="text/javascript"#2
       console.log("post from: {{ post.contributor.username }}");
 #1/script#2
    

Literally post contains not only the post information but any and all the information you want from the contributor foreign key by simple dot notation. Truly beautiful. I'm sure you all knew this already but for me it was a useful realization. Hope it helps somebody!!

Cheers!!

Tommy