I have a Task model:
class Task(models.Model):
text = models.TextField()
datetime = models.DateTimeField(auto_now_add=True)
taken = models.BooleanField(default=False)
done = models.BooleanField(default=False)
client = models.ForeignKey('UserProfile', related_name='tasks_given')
executor = models.ForeignKey('UserProfile', related_name='tasks_received')
def __unicode__(self):
return 'Task #'+str(self.id)
and I have to handle its creation, validation, cancelling etc.
What’s the best way to do it? Is it better to have one (for all cases) or few (for each case) functions in views.py? Is it better to have one template with a lot of {% if %} or is it better to have a few? Any other hints will be appreciated =)
For actions, you’d better extend class-based generic views, they are quite modular, have most of the functionality already baked in and sensible defaults for template names etc.
https://docs.djangoproject.com/en/1.3/ref/class-based-views/#detail-views
They are based on mixin composition, so for each piece or step of functionality there is a very specific method you have to override. Once you get it it is very simple.
For templates, use the
extendsandincludetags and avoid repeated code. Try to use most automatically generated code as you can.