3 ms·
I believe they intended the 'proper' code to look like: def global_feed(request): tweets = Tweet.objects.all() tweets = itertools.ifilter(l
by drostie 14y ago
I believe they intended the 'proper' code to look like:
def global_feed(request):
tweets = Tweet.objects.all()
tweets = itertools.ifilter(lambda t: not t.is_profane(), tweets)
return render(request, 'global_feed.html', {'tweets': tweets})
tweets_per_page = 20
def user_timeline(request, username):
tweets = Tweet.objects.filter(user__username=username)
page = request.GET.get('page', 1)
offset = (page - 1) * tweets_per_page
tweets = tweets[offset:offset + tweets_per_page]
return render(request, 'user_timeline.html', {'tweets': tweets})
- danso 14y agoSo the problem with that is seems to be...what if there was another view that required pagination? So the two unpleasant scenarios seem to be this: 1) The OP's assertion that DRYing the code may unintentionally break functionality in all the places that use it. 2) The DRY assertion: copy-pasting functionality, such as pagination, makes it more likely that the pagination functionality won't be properly updated across all the modules that use it. I guess it's a case of YMMV...because in this hypothetical app, it doesn't seem likely that the number of views will multiply, thus making it easier to update the copy-pasted code. But that seems like a mindset as prone to future problems than one that is more DRY-minded.