Amon
Amon

Reputation: 2951

Can't iterate through Users list with django tags

I'm creating a friends list feature for a website, right now I'm just testing it. So I'm trying to display all Users with a little button beside their name that would send a friend request to them. But when I do it nothing appears on the page

HTML:

<h1>My profile</h1>
    {% for each_user in all_users %}
        <a href="{% url 'change_friends' operation='add' pk=each_user.pk %}"> Click </a>
        <h3>{{ each_user.first_name }}<h3>
    {% endfor %}

views.py

def profile_test(request):
    #going to use this view to display the profile page, and test alterations to it
    form = ImageUploadForm(request.POST, request.FILES)
    user = User.objects.get(id=request.user.id)
    all_users = User.objects.all()
    if request.method == "POST":
        user.userprofile.img = request.FILES.get('uploaded_image')
        user.userprofile.save()
        return HttpResponseRedirect(reverse("profile_test"))
    else:
        print 'invalid'
        form = ImageUploadForm()

    return render(request, 'profile_test.html', {form:'form', user: 'user', all_users:'all_users'})

def change_friends(request, operation, pk):
    #this view will manage a user's friends
    other_user = User.objects.get(pk=pk)
    #a list of primary keys

    if operation == 'add':
        Friend.objects.add_friend(
            request.user, other_user, message='Hi, I would like to add you')

    elif operation == 'remove':
        Friend.objects.remove_friend(request.user, other_user)


    return redirect('profile_test')

Upvotes: 0

Views: 247

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599758

Your context dictionary is the wrong way round: you've swapped the keys and values. It should be:

return render(request, 'profile_test.html', {'form': form, 'user': user, 'all_users': all_users})

Upvotes: 2

Related Questions