In this view, when hashtag only is provided its fine, when sortby is anything except for the handled cases it’s fine, when there’s hashtag AND any sortBy handled (like Popular) nothing is returned when it shouldn’t because all Popular does is sorting and the texts already exist because they’re returned when only hashtag is provided, Here’s the view:
@login_required
def get_texts(request):
updated_texts = []
sortBy = request.GET.get('sortBy')
hashtag = request.GET.get('hashtag')
search_query = request.GET.get('search', '').strip()
base_query = HiveText.objects.all()
if hashtag:
base_query = base_query.filter(caption__icontains=f"#{hashtag}")
if search_query:
base_query = base_query.filter(caption__icontains=search_query)
if sortBy == "Popular":
texts = base_query.order_by("-like_counter", "-created_date")
elif sortBy == "iMessages":
texts = base_query.filter(capturetype="iMessages").order_by("-created_date")
elif sortBy == "Dating Apps":
texts = base_query.filter(capturetype="Dating Apps").order_by("-created_date")
elif sortBy == "Social Media":
texts = base_query.filter(capturetype="Social Media").order_by("-created_date")
else:
texts = base_query.order_by("-created_date")
texts = texts[:20]
for text in texts:
try:
image_filenames = json.loads(text.content)
except json.JSONDecodeError:
image_filenames = []
updated_texts.append({
'item': text,
'image_filenames': image_filenames,
})
return render(
request,
"hive_text.html",
{
"texts": updated_texts,
},
)