Extra data about objects in django templates

Question!

I have django objects:

class Event(models.Model):
   title = models.CharField(max_length=255)
   event_start_date = models.DateField(null=True, blank='true')
   ...

class RegistrationDate(models.Model):
    event = models.ForeignKey(tblEvents)
    date_type = models.CharField(max_length=10, choices=registration_date_type)
    start_date = models.DateField(blank='true', null='true')
    end_date = models.DateField(blank='true', null='true') 

An Event can have early, normal, and late registration periods.

I wrote a function that takes in an event and returns one of: None, "Early", "Normal", or "Late"

All that works great.

In my app, I want to display a list of events and where their registration status is. So I did a query as such.

Events = tblEvents.objects.all()

So I have all of the info about the event, but not the status.

What is the easiest/best way to get the status for each event displayed in the template.

I figure that I can write a template tag, but that seems like more work then should be necessary.



Answers

I think you can make that function you wrote a class method of Event. Then you can just call it from the template. For example...

{% if event %}
    event.getStatus
{% endif %}

...but I haven't done Django in a little while.

By : Pace


This video can help you solving your question :)
By: admin