无法使用django标记遍历用户列表

时间:2017-09-06 21:00:18

标签: python html django

我正在为网站创建朋友列表功能,现在我只是在测试它。所以我试图用名字旁边的一个小按钮显示所有用户,向他们发送朋友请求。但是当我这样做时,页面上没有任何内容出现

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')

1 个答案:

答案 0 :(得分:2)

你的上下文字典是错误的方式:你已经交换了键和值。它应该是:

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