Question

I am trying to get a batch notification to work, but the badge throws several badges with the below code, and it does not show the number. I want the badge to display the number of content within several posts matches with an interest.
I am still a beginner at django, so please bear over with me, if this is a total bad approach.

interest.html

{% for item in interest %}
        <ul class='nav nav-pills nav-stacked'>
            <li><a href='/'>
                <span class='badge pull-right'>
                    {% for word in post %}
                        {% if word == interest %}
                            {{ word.as_number }}
                        {% else %}
                            0
                        {% endif %}
                    {% endfor %}
                </span>
                {{ item.interest }}
            </a></li> 
        </ul>
        {% endfor %}

context_processors.py

def user_interest(request):
    interest = Interests.objects.all()
    interests_form = InterestsForm(request.POST or None)
    post = Posts.objects.all()

    if interests_form.is_valid():
        new_interest = interests_form.save(commit=False)
        new_interest.save()
        #return HttpResponseRedirect('/')
        #apparently it is not needed here


    return {'interest': interest,
            'interests_form': interests_form,
            'post': post,
            }

models.py

class Interests(models.Model):
    interest = models.CharField(max_length=100)
    created = models.DateTimeField(auto_now_add=True)
Was it helpful?

Solution

I'm not fully understanding, but part of your problem may be this part:

{% if word == interest %}

You're comparing a particular Post with all of your Interests (since 'interest' = Interests.objects.all()). At the very least I would change that to:

{% if word == item %}

As this iterates through your interest list (using 'item') and compares it to each 'word' in your post list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top