使用.format将文本插入到某些HTML代码中,获取错误(Python)

时间:2023-01-29 09:01:34

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.

我对Python很陌生,为了练习,我决定创建一个小程序来帮助我爸爸更新他的网站(他拥有一个小电影院)来显示正在播放的电影。

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.

我基本上制作了一堆文本框来输入电影的标题,演员表,播放时间等等。完成后,点击“提交”,它会吐出一个新的index.html页面上传到服务器。

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.

问题是,我正在使用{0},{1},{2}等将文本框中的新信息插入到HTML代码中(这是我程序中的一个字符串),并且Python不断混淆,因为HTML代码中包含一些带花括号的Javascript。

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).

我用 {some javascript stuff}替换了HTML,使其在堆栈上变得更简单(并帮助找到问题的根源)。

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.

当我在我的程序中有实际站点的HTML代码时,它会显示“ValueError:unexpected'{'in field name”并指向函数的结尾(self.text_image.get(1.0,'end'))))) 。当我使用上面的简化版本时,我得到“KeyError:'一些javascript的东西'”,它也指向函数的结尾。

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

我认为如果我可以告诉Python忽略所有的JavaScript花括号,它可能会有效,但是使用转义字符是行不通的。

Hopefully this made sense. Can anyone help?

希望这是有道理的。有人可以帮忙吗?

1 个解决方案

#1


0  

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.

看起来你需要在JavaScript部分中转义花括号。您可以在JavaScript中使用双花括号{{}}而不是单个花括号。

#1


0  

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.

看起来你需要在JavaScript部分中转义花括号。您可以在JavaScript中使用双花括号{{}}而不是单个花括号。