【问题标题】:Passing argument as HTML attribute将参数作为 HTML 属性传递
【发布时间】:2012-08-16 21:54:11
【问题描述】:

我正在尝试使用 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)

【问题讨论】:

    标签: python html bottle


    【解决方案1】:

    你可以试试这样的:

    @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的问题了

    【讨论】:

      【解决方案2】:

      您要查找的是 HTML 和 CSS

      <span style="color:red"><b>This is red</b></span>
      

      使用template制作页面。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-04
        • 1970-01-01
        • 2017-11-19
        • 2011-08-15
        • 1970-01-01
        • 2010-11-25
        相关资源
        最近更新 更多