Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJay Norris
14,824 PointsExclude and timedelta
I've been banging my head against the wall in documentation for 2 days, so I'll ask. This is my first question, so just in case my code doesn't attach like it says, here is the relevant view:
def recent_reviews(request): term = timedelta(days>180) datetime = models.Review.objects.exclude(created_at__timedelta) return render(request, 'products/reviews.html', {'reviews': None})
from datetime import datetime
from django.shortcuts import render
from . import models
def good_reviews(request):
reviews = models.Review.objects.filter(rating__gte=3)
return render(request, 'products/reviews.html', {'reviews': reviews})
def recent_reviews(request):
term = timedelta(days>180)
datetime = models.Review.objects.exclude(created_at__timedelta)
return render(request, 'products/reviews.html', {'reviews': None})
3 Answers
Chris Freeman
Treehouse Moderator 68,454 PointsYou are headed in the right direction.
- Prefix
timedelta
with the module name "datetime.
" - The argument to
timedelta
must be an assignment. Use "(days=180)
" - The comparison to
created_at
must be adatetime
object not atimedelta
object - To get the
datetime
object, subtract thetimedelta
fromnow()
- The comparison to
created_at
needs to be an assignment. - Include the filtered
reviews
result in the context returned
import datetime
def recent_reviews(request):
term = datetime.timedelta(days=180)
six_months_ago = datetime.datetime.now() - term
reviews = models.Review.objects.exclude(created_at__lt=six_months_ago)
return render(request, 'products/reviews.html', {'reviews': reviews})
Jay Norris
14,824 PointsI still can't get it to work. I've tried just pasting what you have in there and it doesn't work. I've also tried many different variations (like using datetime.datetime.today() and changing term to datetime, etc.) I don't see why though. I'm sure it's something obvious. Thank you for your help.
Chris Freeman
Treehouse Moderator 68,454 PointsMy answer relies on importing datetime module:
import datetime
and not the individual objects as you have done:
from datetime import datetime
With your import statement, my answer would need to change "datetime.datetime
" to "datetime
" and change "datetime.timedelta
" to "timedelta
"
You original code didn't import timedelta
Jay Norris
14,824 PointsThere it is. I will not make that mistake again! Thanks!