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