Better solutions for my views?

0 views
Skip to first unread message

Bernd

unread,
Oct 10, 2007, 5:46:43 PM10/10/07
to django...@googlegroups.com
Hello,

I'm working on a website with Django and I'm new to django, python and webdevelopment. I use the latest django svn-version.
I have a few pages/templates and they worked. But I think about to simplify the source-code with the right use of django and python. I think my views are complicated right now and there must/should be a better solution.

Models:

class EventType(models.Model):
    type    = models.CharField(max_length=40, unique=True)
    useable = models.BooleanField()
    crdate  = models.DateTimeField (auto_now_add=True)
    chdate  = models.DateTimeField(auto_now=True)

class Event(models.Model):
    eventdate = models.DateField(core=True)
    eventtype = models.ForeignKey(EventType, limit_choices_to = {'useable': True})
    show_pics = models.BooleanField()
    crdate    = models.DateTimeField(auto_now_add=True)
    chdate    = models.DateTimeField(auto_now=True)

    def get_thumbnail_url(self):
      return Config.objects.get (key='LINK_FLYER_TN').value % \
             {'edate': self.eventdate.strftime('%Y%m%d'),
              'mediaurl': settings.MEDIA_URL}
    # returns an url for  a thumbnail-picture
    # LINK_FLYER_TN = %(mediaurl)sevents/%(edate)s/%edate)s_tn.jpg
    # I don't save the name in an imagefield, because the only difference is the date

class Media(models.Model):
    event       = models.ForeignKey(Event)
    partner     = models.ForeignKey(Partner)
    desc        = models.CharField(max_length=50)
    media       = models.ImageField(upload_to='events/%Y%m%d/presse')
    publishdate = models.DateField()
    show_media  = models.BooleanField()
    crdate      = models.DateTimeField(auto_now_add=True)
    chdate      = models.DateTimeField(auto_now=True)


2 Exercises/Questions:

1... I want to view a thumbnail-list from all events. So I thought I pass a dataset to my view
events = Event.objects.select_related().filter(show_pics=True).order_by('-eventdate')

      But so it's not possible to call:
  {% for event in events %}
       event.get_thumbnail_url()
  {% endfor %}

      Now my solution is to loop through the dataset in a view and build a dictionary with the required information.
      But in that case I loop twice through all events. The first time in the view. The second time in the template :-(

      Does anybody know a better solution without saving the thumbnail-name/url in the database????

2... For the events I have one ore more "Media". I want to create a site with following structure:
 
Heading with Eventinformation
First Mediainformation (related to the event above)
Second Mediainformation (related to the event above)
....
Next Heading with Eventinformation
First Mediainformation (related to the event above)
.....

      Today my solution is the same as above. I loop through events/media in a view and create a dictionary
{[eventinfo, [mediainfo, mediainfo,..]], [eventinfo, [mediainfo, mediainfo,..]],...}


I hope my explanation is understandable and somebody can help me with a better solution. thanks

Regards
  Bernd


Bernd

unread,
Oct 10, 2007, 6:17:17 PM10/10/07
to django...@googlegroups.com
Question 1 is solved. the brackets are wrong. right way is:

{% for event in events %}
event.get_thumbnail_url
{% endfor %}

I would be appreciate if there would be any ideas to my second question


Bernd

John M

unread,
Oct 11, 2007, 12:55:15 AM10/11/07
to Django users
for your second question, i think you could use the ifchanged template
directive on a list of all your media information. Similar to what
you're doing, but you don't have to build a dictionary, the templates
will print when the field on ifchanged changes. something like that I
think.

J

Reply all
Reply to author
Forward
0 new messages