0

I am creating the html for an email using a Python string, like so:

      # Code setting up the message html
      message = "long html message string"

      scoped = ""
      if settings.DEBUG:
          scoped = "scoped"

      header = """                                                                
          <style %s type='text/css'>                                              
              @media only screen and (max-width: 480px){                          
                  .emailImage{                                                    
                      height:auto !important;                                     
                      max-width:200px !important;                                 
                      width: 100% !important;                                     
                  }                                                               
              }                                                                   
          </style>                                                                
          """ % scoped
      footer = "html message footer"

      message = header + message + footer

      # Code sending the message.

The problem is, the above code gives me the error ValueError: too many values to unpack. However, if I remove the scoped variable from the message, the html runs, i.e., this works (albeit without adding the scoped variable into my HTML as I want it to).

      # Code setting up the message html
      message = "long html message string"

      header = """                                                                
          <style type='text/css'>                                              
              @media only screen and (max-width: 480px){                          
                  .emailImage{                                                    
                      height:auto !important;                                     
                      max-width:200px !important;                                 
                      width: 100% !important;                                     
                  }                                                               
              }                                                                   
          </style>                                                                
          """
      footer = "html message footer"

      message = header + message + footer

      # Code sending the message.

Why is the first version throwing that error, and how can I address the ValueError?

1
  • Why are you building up HTML by string substitution? That is what templates are for. Commented Jun 12, 2015 at 6:55

1 Answer 1

4

You have an unescaped % symbol after the width element, add another % to escape it:

  header = """                                                                
      <style %s type='text/css'>                                              
          @media only screen and (max-width: 480px){                          
              .emailImage{                                                    
                  height:auto !important;                                     
                  max-width:200px !important;                                 
                  width: 100%% !important;                                     
              }                                                               
          }                                                                   
      </style>                                                                
      """ % scoped

Note that when you got rid of the % scoped, you were no longer formatting the string and the % character was no longer special.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes! I had earlier tried to escape it with \% and that didn't work - you have to escape using another % in this context?
Yea, in the old style formatting it is very similar to C printf where you also escape % with another %

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.