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开发者_JS百科 = 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 ?
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:
I think I'd have a separate URL like
/items/today/
for this, or perhaps just/today/
.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.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. ;-)
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.
精彩评论