views:

61

answers:

2

Greetings I am hacking Django and trying to test something such as:

Like woot.com , I want to sell "an item per day", so only one item will be available for that day (say the default www.mysite.com will be redirected to that item),

Assume my urls for calling these items will be such: www.mysite.com/item/<number>

my model for item:

class Item(models.Model):
        item_name = models.CharField(max_length=30)
        price = models.FloatField()
        content = models.TextField() #keeps all the html content
        start_time = models.DateTimeField()
        end_time = models.DateTimeField()

And my view for rendering this:

def results(request, item_id):
    item = get_object_or_404(Item, pk=item_id)
 now = datetime.now()

    if item.start_time > now:
     #render and return some "not started yet" error templete
 elif item.end_time < now:
     #render and return some "item selling ended" error templete
 else:
     # render the real templete for selling this item

What would be the efficient and clever model & templete for achieving this ?

+2  A: 

It seems you've got the basics figured out, so I'm assuming you're asking for polishing suggestions... A few ideas in this vein:

  1. I think I'd have a separate URL like /items/today/ for this, or perhaps just /today/.

  2. You'll want to use the date components of datime.datetime.now() only. The whole thing is an object containing the current time specified to a microsecond's precision.

  3. How about using a single base template for all three cases and inheriting from it to change a block containing either the button to click on when buying, the price etc., or a note saying that the item is not being sold yet / any more. Then people can still use the numbered URLs when saying things like See what I bought yesterday, you have to go to that site in an e-mail. ;-)

Michał Marczyk
+1  A: 

I have a photo of the day feature on my site. I have a model that represents today's photo, and a cron job runs a custom management command at midnight to update it with the next photo in the sequence (also a model). So all my view has to do is retrieve the current photo from the database.

Brian Neal
a good idea, but my items will be shown as list under (passed deals) so I guess your idea wouldn't work.
Hellnar
Not sure what you mean. Since I keep the sequence in the database I can still show previous photos of the day. `/photo_of_the_day/` is today's photo while `/photo_of_the_day/42` is a previous one by id.
Brian Neal