url_for function not working in flask application

86 views Asked by At

This is my app.py:

from flask import Flask
from flask_smorest import Api
from db import db
from routes import blp as RoutesBlueprint


def create_app():
    app = Flask(__name__)
    app.config["API_TITLE"] = "URL Shortner"
    app.config['SECRET_KEY'] = 'key'
    app.config["API_VERSION"] = "v1"
    app.config["OPENAPI_VERSION"] = "3.0.3"
    app.config["OPENAPI_URL_PREFIX"] = "/"
    app.config["OPENAPI_SWAGGER_UI_PATH"] = "/swagger-ui"
    app.config[
        "OPENAPI_SWAGGER_UI_URL"
    ] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist/"
    app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"

    #db.init_app(app)
    api = Api(app)
    #with app.app_context():
    #    db.create_all()

    api.register_blueprint(RoutesBlueprint)

    return app

This is my routes file

from flask_smorest import Blueprint
from flask import render_template, session, redirect, url_for
from basic import InfoForm

blp = Blueprint("Routes", __name__, description="The routes of the app")

@blp.route('/', methods = ['GET','POST'])
def index():
    form = InfoForm()
    if form.validate_on_submit():
        session['breed'] = form.breed.data
        session['neutered'] = form.neutered.data
        session['mood'] = form.mood.data
        session['food_choice'] = form.food_choice.data
        session['feedback'] = form.feedback.data

        return redirect(url_for('temp'))
    
    return render_template('index.html', form = form)

@blp.route('/thankyou')
def thankyou():
    return render_template ('thankyou.html')

@blp.route('/temp')
def temp():
    return render_template('temp.html')

My FlaskForm class is this

from flask_wtf import FlaskForm, Form 
from wtforms import (StringField, SubmitField, RadioField, 
                     TextAreaField, BooleanField,
                     DateTimeField, SelectField)
from wtforms.validators import DataRequired
class InfoForm(FlaskForm):
    breed = StringField("What Breed? ", validators=[DataRequired()])
    neutered = BooleanField("Neutered?")
    mood = RadioField('Choose: ', 
                      choices=[("mood_one", 'Happy'), ('mood_two', 'excited')])
    food_choice = SelectField("choose: ", 
                              choices=[('chi',"Chicken"), ('bf','Beef'), ('fish', 'Fish')])
    feedback = TextAreaField("Feedback", default= 'Type in any feedback')
    submit = SubmitField("Submit")

This is my form

<form>
    {{form.hidden_tag}}
    {{form.breed.label}}
    {{form.breed}}
    <br>
    {{form.neutered.label}}
    {{form.neutered}}
    <br>
    {{form.food_choice.label}}
    {{form.food_choice}}
    <br>
    {{form.mood.label}}
    {{form.mood}}
    <br>
    {{form.feedback.label}}
    {{form.feedback}}
    <br>
    {{form.submit}}
    <br>
</form>

My intention was that the url_for in routes.thankyou will redirect the user to a thank you page upon submitting but that is not happening.

temp is a sample route that I created. All templates exist and are in working order.

I checked by manually going to that route and that web page exists and has all the information that was supposed to be embedded in it however the redirection is not taking place.

I am fairly new to flask so assume a beginner mistake. Please ask if any further information is needed

Thank you.

1

There are 1 answers

1
BcK On

You need to use the blueprint name as prefix when referring to routes that are under blueprints. In your case it should be url_for("Routes.temp").

From the documentation:

When using a blueprint, the name of the blueprint is prepended to the name of the function, so the endpoint for the login function you wrote above is 'auth.login' because you added it to the 'auth' blueprint.