質問があります。HTML入力を使用して背景色と前景色を変更する必要があるpython CGIスクリプトをコーディングしています。クッキーを実装するとします。私が直面している問題は、背景色を好きなだけ変更できることですが、前景色を入力すると、入力を変更できず、アプリケーションを再起動する必要があります。これが私のコードです:
<pre>
<code>
#!/usr/bin/python
import cgi
import cgitb
cgitb.enable()
import Cookie
import os
backgroundColor = None
foregroundColor = None
form = cgi.FieldStorage();
if 'background' in form:
C = Cookie.SimpleCookie()
backgroundColor = form.getvalue('background')
C['background'] = backgroundColor
print C
if 'foreground' in form:
D = Cookie.SimpleCookie()
foregroundColor = form.getvalue('foreground')
D['foreground']= foregroundColor
print D
print """Content-Type: text/html
<html>
<head>
<title>Biweekly Task 3</title>
</head>
"""
if backgroundColor:
print """
<body bgColor=%s>
<form method="GET" action="http://localhost:8000/cgi-bin/t3.py">
</form>""" % (backgroundColor)
if foregroundColor:
print """
<body>
<h1 style="color:%s"> Biweekly Task 3 </h1>
<p style="color:%s"> Choose Background Color: <input type="text" name="background">
<br/>
Choose Foreground Color: <input type="text" name="foreground">
<br/>
<input type="submit"></p>
<form method="GET" action="http://localhost:8000/cgi-bin/t3.py">
</form>""" % (foregroundColor, foregroundColor)
else:
print """
<form method="POST" >
<h1> Biweekly Task 3 </h1>
<p>Choose Background Color: <input type="text" name="background">
<br/>
Choose Foreground Color: <input type="text" name="foreground">
<br/>
<input type="submit" value="Change"></p>
</form>
</body>
</html>
"""
</code>
</pre>
誰でも問題を教えて、このコードを修正できますか?