Question 1) When we run a Python script, from the terminal using the “python3” command, the Python interpreter automatically assigns the value “main” to the variable name. Is that true?
Answer: Yes
Question 2) We can have multiple URL paths pointing to the same route function. True or False?
@app.route(‘/some/path’)
@app.route(‘/some/other/path’)
def func():
return ‘Hello World!’
@app.route(‘/some/path’)
@app.route(‘/some/other/path’)
def func():
return ‘Hello World!’
Answer: True
Question 3) Given the following routes, what happens when user accesses the “/home” URL on their browser? Assume the server path is correct i.e. localhost or the correct ip and port.
@app.route(‘/home’)
def home():
return redirect(url_for(‘about’, name=’World’))
@app.route(‘/about/<name>’)
def about(name):
return f’Hello {name}’
@app.route(‘/home’)
def home():
return redirect(url_for(‘about’, name=’World’))
@app.route(‘/about/<name>’)
def about(name):
return f’Hello {name}’
Answer: The string “Hello World” is returned and displayed on user browser
Question 4 ) Given the following code
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
class MyForm(FlaskForm):
name = StringField(‘Name’, validators=[______________])
submit = SubmitField(‘Submit’)
We want the “name” field to be a mandatory/ required field in the form and we want to use the FlaskForm class to create our form. Complete the code: type in the just the text that needs to be written to set an appropriate validator.
Answer: DataRequired()
Question 5) Given the following Data Model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(255))
date_joined = db.Column(db.Date)
What would you change if you were to make username a required column? That is, no User instance can be created without setting a value for username.
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(255))
date_joined = db.Column(db.Date)
What would you change if you were to make username a required column? That is, no User instance can be created without setting a value for username.
Answer: username = db.Column(db.String(255), nullable=False)
Question 6) Given the following code
from flask import render_template
@app.route(‘/home’)
def home():
return ‘<h1>Home Page</h1>’
from flask import render_template
@app.route(‘/home’)
def home():
return ‘<h1>Home Page</h1>’
Answer:
@app.route(‘/home’)
def home():
return render_template(‘home.html’)