2016-09-15 51 views
0

添加glyphicon我有一個燒瓶跆拳道形式如下:瓶WTF形式在表單字段

class URL_Status(Form): 
    url = URLField("Enter URL", 
         validators=[url(), DataRequired()], 
         render_kw={"placeholder": "http://www.example.com"},) 
    submit = SubmitField('Search', render_kw={"onclick": "loading()"}) 

現在我想加在輸入提交即「URL」的自舉glyphicon。據我所知,link。我們需要編寫的代碼如下:

<div class="form-group has-feedback"> 
    <label class="control-label">Username</label> 
    <input type="text" class="form-control" placeholder="Username" /> 
    <i class="glyphicon glyphicon-user form-control-feedback"></i> 
</div> 

當我打電話以HTML形式我的HTML創建的所有代碼,除了這一行:

<i class="glyphicon glyphicon-user form-control-feedback"></i> 

任何建議如何我可以從加入這一行我class'URL_Status',這樣我可以看到我的輸入字段的圖形。謝謝

+0

的可能的複製[添加引導Glyphicon到輸入框(http://stackoverflow.com/questions/18838964/add-bootstrap-glyphicon-to-input-box) –

+0

我沒有創造使用HTML的表單。我使用wtf從服務器端創建表單。 –

+0

我解決了你的問題,試試吧! –

回答

1

我有一段時間沒有使用wtforms。我想你需要一個自定義的widget:

class CustomURLInput(URLInput): 
    ... 
    def __call__(self,....): 
     ... 

看看這個https://github.com/wtforms/wtforms/blob/9be964158fbcd1af52b345451bbd14751127dd37/wtforms/widgets/core.py#L159 瞭解詳情。

和你的URL字段:

url = URLField(
    "Enter URL", 
    validators=[url(), DataRequired()], 
    render_kw={"placeholder": "http://www.example.com"}, 
    widget=CustomURLWidget() 
) 

或者你可以在你的模板做。

1

這是一個小技巧來解決這個問題。

CSS:

.user-icon { 
padding-left:30px; 
background-repeat: no-repeat; 
background-position-x: 4px; 
background-position-y: 4px; 
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABcAAAAWCAYAAAArdgcFAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA+5pVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD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BMDNEMDVDMSIvPiA8ZGM6dGl0bGU+IDxyZGY6QWx0PiA8cmRmOmxpIHhtbDpsYW5nPSJ4LWRlZmF1bHQiPmdseXBoaWNvbnM8L3JkZjpsaT4gPC9yZGY6QWx0PiA8L2RjOnRpdGxlPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PkX/peQAAACrSURBVHja7JSLCYAwDEQbJ3AER+kouoFu0FEcqSM4gk4QE4ggVRPxg1A8OFCSvkqC5xDRaSZ5ciTjyvzuzbMnwKjY34FHAx618yCQXQHAcVFE5+GoVijgyt3UN1/+hPKFd0a9ubxQa6naMjOdOY2jJAdjZIH7tJ8gzRNuZuho5MriUfpLNbhINXk4Cd27pN3AJVqvQlMPSxSz+oegqXuQhz9bNvDpJfY0CzAA6Ncngv5RALIAAAAASUVORK5CYII=);} 

模板:

<form class="form form-horizontal" method="POST"> 
    {{ form.hidden_tag() }} 
    {{ form.username(class_="form-control user-icon") }} 
    {{ form.password }} 
    {{ form.submit }} 
</form> 

該解決方案基於this answer。它只是使用base64 URI編碼將圖像的圖像表示直接嵌入到CSS中。

您可以在此site上獲得glyphicon的base64數據。此外,您還可以使用圖像(25 * 25)替換base64數據。就像這樣:

background-image: url({{ url_for('static', filename='user.png') }});