python - 将参数作为 HTML 属性传递

标签 python html bottle

我正在尝试使用 Bottle 和 HTML 来测试 HTTP GET 和 POST。我编写了这段代码,要求用户输入颜色名称作为参数,如果它存在于预定义列表中,那么它应该打印找到并显示颜色。但我不知道如何通过争论。如果我尝试橙色、红色等默认值,效果很好。

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)

最佳答案

你可以尝试这样的事情:

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

那么剩下的就是template/html/css的问题了

关于python - 将参数作为 HTML 属性传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11988232/

相关文章:

python - 执行没有多处理池映射的进程列表

python - 具有稀疏矩阵的 bool 索引 Numpy 数组

ide - 推荐用于纯网络应用程序的 IDE(HTML5 等)

python - 在 Python Bottle 中返回不同的 mime 类型

python - 以编程方式启用或禁用@auth_basic()

python - SQLAlchemy 版本控制关心类导入顺序

Python网站抓取python并解析数据

html - CSS 仅在第一个元素上溢出省略号

javascript - 自动制表符不适用于中间的文本?

python - 瓶静态文件