Quiz

  1. When we use url_for, how does Flask actually know what the full URL should be? Like if the app gets a new prefix when deployed, how does it automatically update the links?

    Part of the Apache configuration defines some environment variables that Flask looks at. Like this:

    WSGIScriptAlias /cs304people
        /home/cs304flask/wsgi-solution3/people_app/app.wsgi
        process-group=cs304people
    

    We'll look at the People app code next time, but here's a link: people app

  2. I'm wondering how would url_for() know what the URL should be if multiple routes are pointing to the same function name?

    Great question! If multiple routes point to the same thing, that typically means the endpoint is parameterized, in which case, we have to supply the value of the parameter:

    
    @app.route('/people-born-in/')
    def people_born_in_month(month):
    
     ...
            
    # later
    url_for('people_born_in_month',month=6))
    # of maybe
    url_for('people_born_in_month',month=some_value))
    
    

    If it's even more ambiguous, then you'll have to intervene. In the worst case, you might have to supply the prefix.

  3. Could you define more specificially what a static file is and give an example in which url_for() would be used with it?

    Sure! We just did that in the lecture notes, using a static CSS file.

    Another example would be a logo image. So we might have a template like:

    
        <!doctype>
        <html>
            ...
            <link rel='stylesheet' href="{{url_for('static', filename = 'style.css')}}">
            ...
        <body>
            <header>
                <img class="logo" src="{{url_for('static', filename = 'large-logo.png')}}">
            </header>
    
    

    Then, we put the file of CSS code and the logo file into the static folder in our app folder.

  4. None! I think it all makes sense, just need some practice :)

    For sure!