I get a red curve line when using f-string

Viewed 26

def email_verification(email,password):
        message = Mail(
            from_email='hesheitaliabu@gmail.com',
            to_emails=email,
            subject='HEY BABYYYYYYYYYYYYYYYYYYY',
            html_content=f"""
                <!DOCTYPE html>
                <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
    
                <body>
                    <button onclick="myFunction()">Replace document</button>

                <script>
                function myFunction() {
                window.location.href = "email_verify/" + email + "/" + password;
                }
                </script>
                </body>
                </html>
                """)
        Sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('MyAPI'))
        Sg.send(message)

I have define a function called "email_verification" on FLASK to send the email with HTML design. I have used the f-string for the HTML content. But, I get a red curve line under the line of window.location.href = "email_verify/" + email + "/" + password;.

1 Answers

You need to escape literal braces {} in f-Strings in python.

You do this by doubling them. e.g.

def email_verification(email,password):
        message = Mail(
            from_email='hesheitaliabu@gmail.com',
            to_emails=email,
            subject='HEY BABYYYYYYYYYYYYYYYYYYY',
            html_content=f"""
                <!DOCTYPE html>
                <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
    
                <body>
                    <button onclick="myFunction()">Replace document</button>

                <script>
                function myFunction() {{
                window.location.href = "email_verify/" + email + "/" + password;
                }}
                </script>
                </body>
                </html>
                """)
        Sg = sendgrid.SendGridAPIClient(api_key=os.environ.get('MyAPI'))
        Sg.send(message)

Notice here in the syntax highlighting how the body of myFunction is part of the f-String (green) and not a template variable (white) as in your question.

See https://realpython.com/python-f-strings/#braces

Related