#Follow/Unfollow simple its not working:

21 messages · Page 1 of 1 (latest)

thorny breach
#

I can show the list of followers, but the button its not working(sorry for bothering,):

Views:

def userProfile(request, pk):
    user = User.objects.get(id=pk)
    profile = UserProfile.objects.get(user_id=pk)
    rooms = user.room_set.all()#LOS ROOMS CREADOS
    description = UserProfile.objects.get(user=user).description
    #room_messages = user.message_set.all()#MENSAJES EN LOS ROOMS. 
    topics = Topic.objects.all()
    
    if request.method == 'POST':
        current_user_profile= request.user.userprofile
        action = request.POST['follow']
        if action == "unfollow":
            current_user_profile.follows.remove(request.user.userprofile)
        else:
            current_user_profile.follows.add(request.user.userprofile)
        current_user_profile.save()

    context = {'user': user, 'profile':profile, 'rooms':rooms, 'description':description, 'topics':topics }#'room_messages':room_messages,
    return render(request, 'base/profile.html', context)`

HTML:

          <strong>Follows</strong>
          {% for following in user.userprofile.follows.all %}
           <p>@{{ following }} </p>
          {% endfor %}
          {% if profile in profile.follows.all %}
          <button name="follow" value="unfollow" type="submit">
            Unfollow @{{user.username}}
          </button>
          {% else %}
          
          <button name="follow" value="follow" type="submit">
            Follow @{{user.username}}
          </button>
          {% endif %}`
craggy spindle
#

Please format code properly according to #readme-1st
What exactly is "not working" - how do you observe and debug it?

thorny breach
#

The 'if' statement is not working because when I follow someone from the database, the button doesn't even change from 'follow' to 'unfollow'.

craggy spindle
#

mm, wait does it even submit?

thorny breach
#

nono

craggy spindle
#

well, make a form then that does submit ^_^

thorny breach
#

u are right

#

how can i be such an idiot

#

srry, for wasting your time

#

thanks 🙂

craggy spindle
#

yeah, no problem at all

thorny breach
#

After that, it's not changing. I don't know if it's the 'if' statement or my views logic, because nothing works. It doesn't follow the user and the button doesn't change.

craggy spindle
#

Well, submit fires?

thorny breach
#

Yes, at least the page reloads (refreshes).

craggy spindle
#

add print statements to you view to see which code runs and which doesn't

#

also you should see requests in your console

thorny breach
#

yeah thats right

#

It enters the 'else' block and it shows me that 'profile' is the user to follow and 'current_user_profile' is the logged-in user.

craggy spindle
#

which "else" block?
show you code with prints and what console throws.

#

also could add print(f"{request.POST=}")

thorny breach
#

yeah that help a lot thanks, but now i can only follow myself