I'm new to Python and I am wondering what the best way is to format a string with the parts inserted determined by booleans.
I have a long html form string that can have error messages. I only want to show error messages for those parts of the form that did not validate correctly:
form = """Signup
<form method="post">
<table>
<tr>
<td class="label">
Username
</td>
<td>
<input type="text" name="username" value="%(un)s">
</td>
<td class="error">
%(un_err)s
</td>
</tr>
............
</form>"""
When I am processing the post call I have this:
self.response.out.write(form % {"un": username, "pw": password, "pwv": validate, "email": email, "un_err": "", "pw_err": "", "verify_err": "", "email_err": ""})
Each "..._err" message is determined by a separate boolean, for example, if valid_username is false then the output should be:
self.response.out.write(form % {"un": username, "pw": password, "pwv": validate, "email": email, "un_err": "This is not a valid username", "pw_err": "", "verify_err": "", "email_err": ""})
Is there a nice way to do this?