Home > OS >  jinja if statement is not working. I don't know how to fix it
jinja if statement is not working. I don't know how to fix it

Time:02-07

I really need help! The code does not display different values. if the code finds a join item it should display a link, if it does not find a join item it should display a form. Else: display some text.

html:

{%- if join -%}

        <br>
        <br>
        <br>
        <center>
        <a href="/clubs/{{ club.id }}/add_post" style="text-decoration:none; font-size:18px; color:white; padding:10px 20px 10px 20px; background:#134401;">Add Post  </a>
        </center>
{%- elif not join -%}
        <br>
        <br>
        <br>
                <form action="/clubs/{{ club.id }}" method="POST">
            <center>
            <input type="submit" value="Join" style="width:20%; padding: 10px 5px 10px 5px; background:#1d2d0d; color:white; font-size:20px">
            </center>
        </form>
{%- else -%}
<h1>nope</h1>
{%- endif -%}

function:

@application.route('/clubs/<int:id>', methods=['GET', 'POST'])
@login_required
def viewClub(id):
    post = Post.query.filter_by(clubId=id)
    club = Club.query.get_or_404(id)
    join = Join.query.filter_by(clubId=id, userId=current_user.id)


    if request.method == 'POST':
        name = club.name
        user = current_user.username
        userId = current_user.id
        clubId = club.id
        createClub = Join(name=name, clubId=clubId, user=user, userId=userId)
        db.session.add(createClub)
        db.session.commit()
        return redirect('/')
    else:
        return render_template('viewClub.html', club=club, post=post, join=join)

CodePudding user response:

Your database query doesn't return a result of the query, it returns the query itself. This is always true when checked.

You forgot to add a function like .first() or .all() to the query.

join = Join.query.filter_by(clubId=id, userId=current_user.id).first()
  •  Tags:  
  • Related