Ash Pereira Ash Pereira - 22 minutes ago 2
CSS Question

Python Flask Submit Button Not Working

I have two buttons on a page: follow and unfollow, the follow button is displayed if the user is not following a person and vice versa. I have the following code but for some reason only the unfollow button works. The follow button doesn't do anything - I click on it and nothing happens.

if request.method == 'POST':
if request.form['submit'] == 'Follow':
c.execute("INSERT INTO users_followers(username,following) VALUES(?,?)",(session['username'],username,))
elif request.form['submit'] == 'Unfollow':
c.execute("DELETE FROM users_followers WHERE username = ? AND following = ?",(session['username'],username,))
conn.commit()
return redirect(url_for("profilepage",username=username))


HTML Code:

{%if profile[0][1] == current%}
{%else%}
{% if following == True %}
<form action="/profile/{{username}}" class="form" method="post">
<input class="btn btn--sm type--uppercase" name="submit" style="background: #e74c3c;border-color: #c0392b;color:white;width:100px;" type="submit" value="Unfollow">
{%else%}
<input class="btn btn--sm type--uppercase" name="submit" style="background: #4a90e2;border-color: #4a90e2;color:white;width:100px;" type="submit" value="Follow">
{%endif%}
<a class="btn btn--sm btn--primary-2 type--uppercase" href="#"><span class="btn__text">Send Message</span></a>
</form>
{%endif%}

Answer

You are not printing the form tag if following is not true. Move the following code out of the If statement.

<form action="/profile/{{username}}" class="form" method="post">