[Python #19] [Django #12] 穿上CSS搜索功能 by june0620

View this thread on steempeak.com
· @june0620 ·
$21.52
[Python #19] [Django #12] 穿上CSS搜索功能
pixabay
https://cdn.pixabay.com/photo/2019/03/12/20/27/kanban-4051777_1280.jpg
***

上期只使用了HTML form来显示搜索框,只有骨架没有肉体。
今天就给这个骨架穿上CSS及简单的动态。 

#### 构想
1. 在主页上部显示一个大搜索框,这个搜索框只限搜索texts的搜索,隐藏掉tag和title搜索。
2. 这个搜索框需要容纳多个关键词,则需要js动态来控制负数参数。
3. 添加一个[搜索更多]按钮,点击时显示tags,titles,texts三个搜索框。

但对一个不太懂CSS与JS的初学者来说,实现这些功能非常艰难。
所以呢~~~ 我就借用开源的力量,网上应该有很多开源的代码。 

###### 探索开源1
如我所料,发现网上有很多 Bootstrap 例子。
其中https://colorlib.com/wp/free-css3-html5-search-form-examples/ 的v4最合我意,都实现好了的复数搜索框。
用它可以一并解决上述1,2,就你了。
![](https://cdn.steemitimages.com/DQmQukh3k1T2ungVeFwqvT1WecURkGGVmkzZQdsbFwpZGAn/image.png)

###### 探索开源2
w3schools有例子可以解决我第三个问题了。 
https://www.w3schools.com/howto/howto_js_collapsible.asp

两个开源代码来之不易,我得怎么料理它们呢?
因技术有限无法实现太复杂的UI,只能简单的实现了,上层大搜索框,下层显示‘搜索更多’按钮,点击按钮时第三行显示选项。背景颜色给我喜欢的蓝,黄,红,绿单色。OK 

#### 实现
首先在项目根目录创建一个static文件夹,在之下创建三个文件夹叫 css, images, js 并把开源代码放在其中。 
![](https://cdn.steemitimages.com/DQmYci2Cf1M6XyaQqz53rkkSSKWttad9merFysHdES5BgMY/image.png)

此后,用django的设置来读取static文件夹。在settings.py文件设置如下。 
```
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]
```
<p></p>

![](https://cdn.steemitimages.com/DQmdgHuxTVzcrmJGBUz4hCHzBXBZZMn1Hjdo9uzhv8PxTpL/image.png)


검색 기능은 메인에 노출해야 하므로 base.html에 코드를 추가하자. 주의할 점은 `{% load static %}`로 static 파일을 읽어야 한다. 또한 `<link href="{% static 'css/main.css' %}" rel="stylesheet" />`로 css 파일을 읽어온다. 

```
{% load static %}
{% load post_extras %}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Poppins:400,800" rel="stylesheet" />
<link href="{% static 'css/main.css' %}" rel="stylesheet" />

{% spaceless %}
{% endspaceless %}

<html lang="en">
  <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
      <title>D_Blog</title>
      
  </head>
  <body>
      <header>
          <nav>
              <div class="s004">
              <form action="/{{ request.path | get_account_form_url }}/search/" method="get">
                  <div class="inner-form">
                    <div class="input-field">
                      <input class="form-control" id="choices-text-preset-values" name="texts" type="text" placeholder="Type to search..." />
                        <button class="btn-search" type="submit">
                            <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24">
                          <path d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
                            </svg>
                        </button>
                      </div>
                  </div>
              </form>
              </div>
              <div class="collapsible-container">
                  <button class="collapsible">Advanced search</button>
                  <div class="content">
                      <form class="adv-form" action="/{{ request.path | get_account_form_url }}/search/" method="get">
                          <input id="tags" type="text" name="tags" value="" placeholder="tags">
                          <input id="titles" type="text" name="titles" value="" placeholder="titles">
                          <input id="texts" type="text" name="texts" value="" placeholder="texts">
                          <input type="submit" value="Search">
                      </form>
                  </div>
              </div>
          </nav>
      </header>

      <script src="{% static 'js/choices.js' %}"></script>
      <script>
          var textPresetVal = new Choices('#choices-text-preset-values', {
              removeItemButton: true,
          });
          
          var coll = document.getElementsByClassName("collapsible");
          var i;
          for (i = 0; i < coll.length; i++) {
              coll[i].addEventListener("click", function() {
                  this.classList.toggle("active");
                  var content = this.nextElementSibling;
                  if (content.style.maxHeight){
                      content.style.maxHeight = null;
                  } else {
                      content.style.maxHeight = content.scrollHeight + "px";
                  }
              });
          }
      </script>
{% block content %}
{% endblock content %}
    </body>
</html>

```

#### 结果 

**输入多个关键词 & 第三行隐藏**
![](https://cdn.steemitimages.com/DQmdoT1qkephkiGq81yoaSHeWLcGDGwgcX49yLPwqdQciRt/image.png)

**第三行显示**
![](https://cdn.steemitimages.com/DQmWpHyEX9GfbMmJCcytWyh9tn6xompMQZCu7b151kZHSCz/image.png)

背景色太耀眼?,以后再改改吧。  

***
**[Cookie 😅]**
Python 3.7.4
Django 2.2.4
steem-python 1.0.1
goorm IDE 1.3
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 90 others
👎  ,
properties (23)
post_id87,521,952
authorjune0620
permlinkpython-19-django-12-css
categoryhive-101145
json_metadata{"tags":["hive-101145","cn","mini","zzan","sct-cn","sct-freeboard","dblog","dev","sct"],"image":["https:\/\/cdn.pixabay.com\/photo\/2019\/03\/12\/20\/27\/kanban-4051777_1280.jpg","https:\/\/cdn.steemitimages.com\/DQmQukh3k1T2ungVeFwqvT1WecURkGGVmkzZQdsbFwpZGAn\/image.png","https:\/\/cdn.steemitimages.com\/DQmYci2Cf1M6XyaQqz53rkkSSKWttad9merFysHdES5BgMY\/image.png","https:\/\/cdn.steemitimages.com\/DQmdgHuxTVzcrmJGBUz4hCHzBXBZZMn1Hjdo9uzhv8PxTpL\/image.png","https:\/\/cdn.steemitimages.com\/DQmdoT1qkephkiGq81yoaSHeWLcGDGwgcX49yLPwqdQciRt\/image.png","https:\/\/cdn.steemitimages.com\/DQmWpHyEX9GfbMmJCcytWyh9tn6xompMQZCu7b151kZHSCz\/image.png"],"links":["https:\/\/colorlib.com\/wp\/free-css3-html5-search-form-examples\/","https:\/\/www.w3schools.com\/howto\/howto_js_collapsible.asp"],"app":"steemcoinpan\/0.1","format":"markdown","canonical_url":"https:\/\/www.steemcoinpan.com\/@june0620\/python-19-django-12-css"}
created2020-09-15 12:14:00
last_update2020-09-15 12:14:00
depth0
children10
net_rshares48,711,424,663,339
last_payout2020-09-22 12:14:00
cashout_time1969-12-31 23:59:59
total_payout_value11.326 SBD
curator_payout_value10.189 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length5,103
author_reputation219,336,602,045,061
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
author_curate_reward""
vote details (156)
@sct.krwp ·
@june0620 transfered 1.0 KRWP to @krwp.burn. voting percent : 0.69%, voting power : 79.85%, steem power : 1912853.02, STU KRW : 1200. 
@june0620 staking status : 50 KRWP
                                @june0620 limit for KRWP voting service : 0.1 KRWP (rate : 0.002)
                                What you sent : 1.0 KRWP
                                Refund balance : 0.9 KRWP [46890755 - 487a0c6545b65bc1bd4aad0c45bef3d59efb7492]
properties (22)
post_id87,521,986
authorsct.krwp
permlinkre--python-19-django-12-css-20200915t121639449z
categoryhive-101145
json_metadata{"tags":["hive-101145","cn","mini","zzan","sct-cn","sct-freeboard","dblog","dev","sct"]}
created2020-09-15 12:16:42
last_update2020-09-15 12:16:42
depth1
children0
net_rshares0
last_payout2020-09-22 12:16:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length436
author_reputation10,000,000,000,000
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@upvu ·
<div class='shr_item' style='position:relative; min-height:158px; margin:12px 0; padding:9px 10px; border:1px solid #dbdbdb; border-radius:1px; background-color:#fff;'><div class='item_dtl' style='position:relative; height:158px; padding:5px; border:1px solid #f0f1f4;'><span class='thmb' style='float:left; overflow:hidden; width:156px; height:156px; margin-right:9px; border:1px solid #e7e7e7;'><a href='https://qoo.tn/A3bPVR/Q163214049'><img src='https://steemitimages.com/240x0/https://gd.image-gmkt.com/li/951/279/1472279951.g_400-w-st_g.jpg' width='156' alt='' style='vertical-align: middle; border: 0 none;'></a></span><p class='tit' style='overflow:hidden; max-height:68px; margin-bottom:7px; line-height:17px; color:#000;'>[US$18.50](▼39%)[Xiaomi]🚨2020 최신 항바이러스 필터 입고!!🚨 샤오미 공기청정기 필터 / 미에어 필터 / 고효율 정화 / 활성탄 필터 / 포름알데히드 제거 / 고밀도 헤파필터 / 무료배송</p><span class='url' style='position:absolute; left:170px; bottom:10px; display:block; font-weight:bold; color:#9197a3;'>WWW.QOO10.COM</span></div></div>
<a href="https://coupa.ng/bHgxVT" target="_blank"><img src="https://ads-partners.coupang.com/banners/273559?subId=&traceId=V0-301-bae0f72e5e59e45f-I273559&w=728&h=90" alt=""></a>
properties (22)
post_id87,521,990
authorupvu
permlinkre--python-19-django-12-css-20200915t121649553z
categoryhive-101145
json_metadata{"tags":["hive-101145","cn","mini","zzan","sct-cn","sct-freeboard","dblog","dev","sct"]}
created2020-09-15 12:16:51
last_update2020-09-15 12:16:51
depth1
children0
net_rshares0
last_payout2020-09-22 12:16:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length1,183
author_reputation27,754,495,235,856
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@bo022 ·
拍拍
properties (22)
post_id87,522,096
authorbo022
permlinkqgp970
categoryhive-101145
json_metadata{"app":"steemcn\/0.1"}
created2020-09-15 12:25:48
last_update2020-09-15 12:25:48
depth1
children1
net_rshares0
last_payout2020-09-22 12:25:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length2
author_reputation105,250,028,527,773
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@june0620 ·
谢谢~ 回拍 ^^
properties (22)
post_id87,523,033
authorjune0620
permlinkqgpcw2
categoryhive-101145
json_metadata{"app":"steemcn\/0.1"}
created2020-09-15 13:45:39
last_update2020-09-15 13:45:39
depth2
children0
net_rshares0
last_payout2020-09-22 13:45:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length9
author_reputation219,336,602,045,061
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@ravenkim ·
@tipu curate
properties (22)
post_id87,522,842
authorravenkim
permlinkqgpc1e
categoryhive-101145
json_metadata{"users":["tipu"],"app":"steemit\/0.2"}
created2020-09-15 13:27:15
last_update2020-09-15 13:27:15
depth1
children2
net_rshares0
last_payout2020-09-22 13:27:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length12
author_reputation244,843,674,682,222
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@tipu ·
<a href="https://tipu.online/curator?ravenkim" target="_blank">Upvoted  &#128076;</a> (Mana: 4/6) <a href="https://tipu.online" target="_blank">Passive income with @tipU</a> :)
properties (22)
post_id87,522,844
authortipu
permlinkre-qgpc1e-20200915t132730
categoryhive-101145
json_metadata{}
created2020-09-15 13:27:33
last_update2020-09-15 13:27:33
depth2
children0
net_rshares0
last_payout2020-09-22 13:27:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length176
author_reputation55,804,171,747,699
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@june0620 ·
앗! 감사합니다^^
properties (22)
post_id87,523,026
authorjune0620
permlinkre-ravenkim-qgpcut
categoryhive-101145
json_metadata{"tags":["hive-101145"],"app":"steempeak\/2020.07.1"}
created2020-09-15 13:44:54
last_update2020-09-15 13:44:54
depth2
children0
net_rshares0
last_payout2020-09-22 13:44:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length10
author_reputation219,336,602,045,061
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@annepink ·
补拍完👏🏻
俊俊几天没更新了… 上哪里浪去了🤭
properties (22)
post_id87,566,196
authorannepink
permlinkqguqvt
categoryhive-101145
json_metadata{"app":"steemcn\/0.1"}
created2020-09-18 11:35:57
last_update2020-09-18 11:35:57
depth1
children2
net_rshares0
last_payout2020-09-25 11:35:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length23
author_reputation160,119,580,804,298
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@june0620 ·
哈哈,回来了~
最近有点忙,也没有可写的文章 😂 
给萍萍补去~ 
properties (22)
post_id87,591,814
authorjune0620
permlinkre-annepink-qgy040
categoryhive-101145
json_metadata{"tags":["hive-101145"],"app":"steempeak\/2020.07.1"}
created2020-09-20 05:48:06
last_update2020-09-20 05:48:06
depth2
children1
net_rshares0
last_payout2020-09-27 05:48:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length33
author_reputation219,336,602,045,061
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000
@annepink ·
谢谢俊🥰
properties (22)
post_id87,853,342
authorannepink
permlinkqhqaaj
categoryhive-101145
json_metadata{"app":"steemcn\/0.1"}
created2020-10-05 12:20:45
last_update2020-10-05 12:20:45
depth3
children0
net_rshares0
last_payout2020-10-12 12:20:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 SBD
curator_payout_value0.000 SBD
pending_payout_value0.000 SBD
promoted0.000 SBD
body_length4
author_reputation160,119,580,804,298
root_title"[Python #19] [Django #12] 穿上CSS搜索功能"
beneficiaries[]
max_accepted_payout1,000,000.000 SBD
percent_steem_dollars10,000