Nathan R
Nathan R

Reputation: 870

Using .format to insert text into some HTML code, getting errors (Python)

I'm pretty new to Python, and for practice I decided to create a little program to help my dad update his website (he owns a small movie theater) to show what movies are playing.

I basically made a bunch of text boxes to enter the movie's title, cast, times playing, etc. When you're done, hit "Submit" and it spits out a new index.html page to upload to the server.

The problem is, I'm using {0}, {1}, {2} etc. to insert the new information from the textboxes into the HTML code (which is a string in my program), and Python keeps getting confused because the HTML code has some Javascript with curly braces in it.

Here's the code:

    def submit(self):
        self.createHTML(('''<lots of html>{some javascript stuff}{0}{1}{2}{3}{4}{5}{6}{7}
'''.format((self.text_title.get(1.0,'end')),
           (self.text_date2d.get(1.0,'end')),
           (self.text_date3d.get(1.0,'end')),
           (self.text_cast.get(1.0,'end')),
           (self.text_summary.get(1.0,'end')),
           (self.text_runtime.get(1.0,'end')),
           (self.text_trailer.get(1.0,'end')),
           (self.text_image.get(1.0,'end'))))) 

I replaced the HTML with <lots of html>{some javascript stuff} to make it simpler here on stack (and to help get to the root of the problem).

When I have the actual site's HTML code in my program, it says "ValueError: unexpected '{' in field name" and points to the end of the function (self.text_image.get(1.0,'end'))))). When I use the simplified version above, I get "KeyError: 'some javascript stuff'", and it also points to the end of the function.

I think it might work if I can tell Python to ignore all the JavaScript curly braces, but using escape characters isn't working.

Hopefully this made sense. Can anyone help?

Upvotes: 0

Views: 986

Answers (1)

winhowes
winhowes

Reputation: 8065

It looks like you need to escape your curly braces in the JavaScript portion. You can do by using the double curly braces {{ }} instead of single curly braces in your JavaScript.

Upvotes: 1

Related Questions