user6210879
user6210879

Reputation: 51

Django- ListView returning a list of users with a given tag in a tag field

I'm trying to return a list of users with a particular skill and skills is a TagField (django taggit) in a CustomUser model. I'm struggling to get the queryset right in my ListView (skill_list.html). I want to be able to click on the skill listed on a user's profile (profile.html) and then have that return the skill list page with a list of all users that have that skill.

models.py:

class CustomUser(AbstractUser):
    objects = CustomUserManager()
    position = models.CharField(max_length =200, null=True, default='', 
    blank=True)
    bio = models.CharField(max_length=400, null=True, default='', 
    blank=True)
    skills = TaggableManager(help_text="A comma-separated list of tags.")

views.py:

class SkillView(ListView):
model = CustomUser
template = 'skill_list.html'
queryset = CustomUser.objects.all()

def get_queryset(self):
    queryset = CustomUser.objects.filter(skills__name__in= 
    [self.kwargs['skill']])
    return queryset

profile.html:

<div class="container-fluid" id="profile_container">

            <div class="container skills">
            {% for skill in user.skills.all %}
                <div class="skill_bubble"><p class="skill_bubble"><a href=" 
{% url 'skills' %}">{{ skill.name }}</a></p></div>
            {% endfor %}
            </div>
        </div>

skill_list.html:

<div class="container">

    {% for user in object_list %}

        <div class="container user_name">
        <p class="profile_name"><a href="{% url 'profile_with_pk' 
pk=user.pk %}">{{ user.first_name }}&nbsp{{ user.last_name }}</a></p>
        <p class="profile_text">{{user.position}}</p>
</div>
</div>

I have the url set up on the profile page to return the 'skill_list.html', however I get an key error on the skill_list page: Exception value "skill."

Upvotes: 0

Views: 267

Answers (3)

Alasdair
Alasdair

Reputation: 308979

I want to be able to click on the skill listed on a user's profile (profile.html) and then have that return the skill list page

In that case, the URLs need to include the skill in them, e.g. /skills/python/ or /skills/sql/.

You can do this by changing the URL to something like:

path('skills/<slug:skill>', views.SkillView.as_view(), name='skills')

Now self.kwargs['skill'] will work in the SkillView.get_queryset method.

You now need to include the skill in the URL tag, for example:

{% url 'skills' skill %}

Finally, since you are only using a single item in your list,

queryset = CustomUser.objects.filter(skills__name__in=[self.kwargs['skill']])

you can remove the __in and change the query to:

queryset = CustomUser.objects.filter(skills__name=self.kwargs['skill'])

Upvotes: 1

NduJay
NduJay

Reputation: 810

Then why dont you just use a 'Q' search query. Check it in the docs

Upvotes: 0

NduJay
NduJay

Reputation: 810

I think it should be :request.user instead of user in profile.html

Upvotes: 0

Related Questions