last day (15 days later) » 

20:44
0
A: Python/Django Check if object is in 2 lists

karthikrYou can do: has_tag = set(list(top_tags)).issubset(list(profile_tags)) This gives you the boolean flag.

I'm given a TypeError:'bool' object is not iterable
sorry.. the list() wrap was my mistake.
@karthikr When rendering to the template: I have {% if has_tag %}Remove{% else %}Add{% endif %}. But it seems to never hit the else to show 'Remove'. Would you know how to make the check work?
check the value of has_tag - the template should go by the default True or False boolean. One way to check is - just do {{has_tag}} and see the value
@karthikr I'm sorry that's still confusing for me. I understand {{ has_tag }} would return a boolean value. But it doesn't in the template. I guess what I'm asking is, where does the if part of things come into the scenario? As to say, if its true or if it's false
@karthikr i'm rendering context['has_tag'] = has_tag to the template
20:44
well.. what you are doing is right {% if has_tag %}Remove{% else %}Add{% endif %} would check if has_tag is present or not, and show the result. One more thing I would check is if has_tag is indeed True, or False - do a print and check on the console
It's returning false. But I know that it should return True, as there are profile tags in top_tags
then i guess that is the issue - are you sure you are doing the is_subset properly ?
has_tag = set(list(top_tags)).issubset(list(profile_tags))
issubset() or is_subset?
issubset is correct. the question i am asking is, this checks if top_tags is a subset of profile_tags
is that correct ? or do you want profile_Tags to be a subset of top tags ?
it makes a difference.
20:53
Also, you might just want to hardcode context['has_tag'] to True and False manually, and see if it is correctly showing on the template
you might have interchanged the condition to show Remove v/s Add
Changed the subset. Now it's returning True
oh.. great.. so that is the bug. i shall edit the ansewr
Although it's showing True for every tag in that list :\
just replace your entire logic :
has_tag = False
profile_tags = profile.tags.all()
top_tags = Tag.objects.all()
for top in top_tags:
if top in profile_tags:
has_tag = True
with has_tag = set(list(top_tags)).issubset(list(profile_tags))
has_tag = set(list(top_tags)).issubset(list(profile_tags)) is all you need
you dont need the for loops, etc
thats all i have
20:56
Also, you might just want to hardcode context['has_tag'] to True and False manually, and see if it is correctly showing on the template
you might have interchanged the condition to show Remove v/s Add
didn;t interchange
Your answer is correct. It's just for my case it doesn't work. Thank you for your assistance.
it should.. did you try the hardcoding values to context['has_tag'] = False, and True and testing if each case works ?
and it did not work ? then It much be caching. Just resratrt the local server
Im rendering a list of {% for tag in top_tags|slice:':10' %}
i dont know if that matters or not
21:02
Now I know your mistake -
do this {% for tag in top_tags | slice: 10 %}{% if tag in profile_tags %}Remove{% else %}Add{% endif %}{% endfor %}
You cannot have one check in your case. YOu need to check forindividual elements, and print Remove or Add depending on the element is present in profile_tags
I gave you an answer based on wha tyou asked me.
dint know thi s is the context
Im sorry I didnt provide more information
You're a genius
thank you so much
I marked it as correct, as you were right
Completely my fault
Again thank you
glad it helped :)
good luck. feel free to reach out if yo uhave questions, [email protected]
you're amazing

last day (15 days later) »