-1

I am creating a gym website for a project and I am getting this error:

werkzeug.routing.BuildError: Could not build url for endpoint 'memberships'. Did you mean 'membershipsub' instead?

I had created a template called 'memberships' initially and renamed it to 'membershipsub' after getting this error (everywhere in all the files it was referenced) and still get this error.

This is my code for the building of the template:

@app.route("/memberships", methods = ['GET', 'POST'])
def membershipsub():
    form = MembershipsubForm()
    plan1 = Membership_plan.query.filter_by(id=1).first()
    plan2 = Membership_plan.query.filter_by(id=2).first()
    plan3 = Membership_plan.query.filter_by(id=3).first()
    plan4 = Membership_plan.query.filter_by(id=4).first()
    plan5 = Membership_plan.query.filter_by(id=5).first()
    plan6 = Membership_plan.query.filter_by(id=6).first()
    plan7 = Membership_plan.query.filter_by(id=7).first()
    plan8 = Membership_plan.query.filter_by(id=8).first()
    plan9 = Membership_plan.query.filter_by(id=9).first()
    if form.validate_on_submit():
        age18 = ['18+ year', '18+ 6 month', '18+ month']
        age15 = ['15-17 year', '15-17 6 month', '15-17 month']
        age10 = ['10-14 year', '10-14 6 month', '10-14 month']
        if not (current_user.is_authenticated):
            flash('You need to have an account to subscribe to a membership', 'danger')
            return redirect(url_for('register'))
        elif (current_user.age >= 18) and (form.membershipsub.data not in age18):
            flash('You are not in the required age category to subscribe to this membership or this membership option is not available', 'danger')
        elif (14 < current_user.age < 18) and (form.membershipsub.data not in age15):
            flash('You are not in the required age category to subscribe to this membership or this membership option is not available', 'danger')
        elif (9 < current_user.age < 15 ) and (form.membershipsub.data not in age10):
            flash('You are not in the required age category to subscribe to this membership or this membership option is not available', 'danger')
        else:
            current_user.membership_plan = form.membershipsub.data
            current_user.membership_expiration_date = membership_expiration()
            db.session.commit()
            flash('You have now subscribed to this membership!', 'success')
            return redirect(url_for('mymembership'))       
    return render_template('membershipsub.html', title='Memberships', form=form, plan1=plan1, plan2=plan2, plan3=plan3, plan4=plan4, plan5=plan5, plan6=plan6, plan7=plan7, plan8=plan8, plan9=plan9)

Also, I am not using blueprints so that isn't the issue.

davidism
  • 121,510
  • 29
  • 395
  • 339
  • it's just due to renaming of templates. Try to re-create the project with updated name and see if it's still persist. – Sajjan Kumar Jan 03 '22 at 14:03

1 Answers1

0

Your issue has nothing to do with template names. Most of the information you need is in the error message:

  • The url for for the endpoint "memberships" could not be built, often the case when the endpoint (a route) does not exist or can otherwise not be found.
  • You get a suggestion for an endpoint name that does exist "membershipsub" (the function in your shared code snippet)

If you have no code that looks like the snippet below, that would explain the error you are getting:

@app.route('/memberships')
def memberships():
    return render_template('template_name.html')

You will have to go though your code and find the call to url_for('memberships') that is causing the issue and update it to refer to an existing route.

It is not clear from your question which call to url_for causes the issue. There are 2 calls to url_for in the code snippet you shared but they refer to "register" and "membership" while your error message refers to "memberships" So either the snippet needs updating or the call to url_for is in your template code, which you have not shared.

While your question is not an exact duplicate, the accepted answer here explains most of what you need to know about how url_for works. (and alternative ways of using it)

Feel free to update your question with additional info I can always update or remove this answer.

Maarten
  • 492
  • 7
  • 14
  • Thanks a lot. Turns out that I had used url_for('memberships') in my template for the general layout of the website. It had not shown up when I performed a search on my files for whatever reason. – S1edgehammer500 Jan 03 '22 at 14:51