Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass data to a template in Django?

In the template below, I am trying to get the name of the court (that is captured through the "establishment" field). I am passing "club_id" through the avail_times function, but how do I pass the "establishment" field passed through the template from this?

The model:

class Club(models.Model):
    establishment = models.CharField(max_length=200)
    address = models.CharField(max_length=200)
    def __unicode__(self):
        return self.establishment

class Available(models.Model):
    club = models.ForeignKey(Club)
    court = models.CharField(max_length=200)
    avail_time = models.DateTimeField('available time')
    def __unicode__(self):
        return self.court

The function:

def avail_times(request, club_id):
courts = Available.objects.filter(club__id=club_id)
return render_to_response('reserve/templates/avail_times.html', {'courts': courts})

The template:

    <h1>All available courts for {{ court.club }}</h1>
<ul>
{% for court in courts %}
    <li>{{ court }}</li>
{% endfor %}
</ul>
like image 240
sharataka Avatar asked May 15 '12 12:05

sharataka


People also ask

How do I pass a Django template?

How do you pass a Python variable to a template? And this is rather simple, because Django has built-in template modules that makes a transfer easy. Basically you just take the variable from views.py and enclose it within curly braces {{ }} in the template file.

Can we pass list in Django template?

Django templates not only allow passing data from view to template, but also provides some limited features of a programming such as variables, for loops, comments, extends etc.


1 Answers

def avail_times(request, club_id):
  courts = Available.objects.filter(club__id=club_id)      
  if courts.count() > 0:
    club_name = courts[0].club.establishment
  else:
    # no results found for this club id!

# perhaps it is better to check explicity if the club exists before going further, 
# eg. Club.objects.get(pk=club_id)
# is club_id passed in as a string? I haven't used django in awhile somethign 
# worth looking into?

return render_to_response('reserve/templates/avail_times.html', {'courts': courts, 'club_name': club_name})


<h1>All available courts for {{ club_name }}</h1>
<ul>
  {% for court in courts %}
    <li>{{ court.club.establishment }}</li>
  {% endfor %}
</ul>

You span foreign key relationships using dot notation. You have to go "through" the foreign key to get to the Club model. This is done through accessing the club attribute. So additionally supposed you wanted to access both the establishment name AND the address, you could add <li>{{ court.club.address }}</li> to display the address too.

Be careful though, you can use django debug toolbar to see how many queries are being executed. if you have a lot of courts you might notice a performance hit. Just something to keep in mind.

Courts is a queryset object. You are trying to access property of court.club which doesn't exist, as you've probably noticed django fails silently when this happens. There are a couple ways of getting club name.

You might want to give some thoughts to your schema. Can clubs have multiple courts?? If not it is "better" to use .get as Zahir suggested. If it can have multiple courts then you should look into a ManyToMany relationship to better reflect this.

like image 148
dm03514 Avatar answered Sep 20 '22 15:09

dm03514