我正在尝试使用Bottle和HTML来测试HTTP GET和POST。我编写了这段代码,要求用户输入颜色名称作为参数,如果它出现在预定义列表中,那么它应该打印找到并显示颜色。但我不知道如何通过论证。如果我尝试像Orange,Red等默认值,它可以正常工作。
from bottle import*
import socket
@error(404)
def error404(error):
return '<p align=center><b>Sorry, a screw just dropped.Well, we are hoping to find it soon.</b></p>'
@get('/New/rem_serv/:arg')
def nextstep(arg):
_colorlist=['Red','Green','Blue','Yellow','Orange','Black','White']
if arg in _colorlist:
return "Found the same color \n","<p style='font-weight:bold; text-align:center; background-color:arg;'>" + str(arg)
else:
return error404(404)
addrIp = socket.getaddrinfo(socket.gethostname(), None)
addrIp = addrIp[0][4][0]
run(host=addrIp, port=80)
答案 0 :(得分:2)
你可以尝试这样的事情:
@app.route('/New/rem_serv/:arg')
@view('template.tpl')
def nextstep(arg):
_colorlist=['Red','Green','Blue','Yellow','Orange','Black','White']
if arg in _colorlist:
context = {'result': "Found the same color %s" % arg}
else:
context = {'result': "color not found"}
return (context)
你也可以试试这个:
from bottle import Bottle, run, view, request
app = Bottle()
@app.route('/New/rem_serv/')
@view('template.tpl')
def nextstep():
"""
get the color from the url
http://127.0.0.1:8080/New/rem_serv?color=xxx
"""
_colorlist=['Red','Green','Blue','Yellow','Orange','Black','White']
if arg in _colorlist:
context = {'result': "Found the same color %s" % request.params.color}
else:
context = {'result': "color not found"}
return (context)
然后其余的是模板/ html / css
的问题答案 1 :(得分:1)