2012-08-16 177 views
1

我正在嘗試使用Bottle和HTML來測試HTTP GET和POST。我已經寫了這個代碼,它要求用戶輸入一個顏色名稱作爲參數,如果它存在於預先定義的列表中,那麼它應該打印找到並顯示顏色。但我不知道我如何通過這個論點。如果我嘗試像橙色,紅色等默認值它工作正常。將參數作爲HTML屬性傳遞

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) 

回答

1

你在找什麼是HTML和CSS

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

使用template做網頁。

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的問題

相關問題