create account

[Python #17] [Django #10] 검색 기능에 UI 추가 by june0620

View this thread on: hive.blogpeakd.comecency.com
· @june0620 ·
$3.05
[Python #17] [Django #10] 검색 기능에 UI 추가
pixabay
https://cdn.pixabay.com/photo/2019/05/14/17/07/web-development-4202909_1280.png

검색 기능에 UI를 추가한다. HTML의 form으로 우선 먼저 가볍게 구현해 보자. (깊게 구현할 능력도 안됨ㅎㅎ)

##### form 추가
먼저 django의 templates 폴더의 base.html 에 form 을 넣는다. 
1. action은 일단 하드코딩으로 `/@june0620/search`로 한다. 계정명은 변수로 처리하고 싶은데 안된다. ㅠ (#연구필요#장고초자)
2. 검색 기능이니 method는 post보다는 get이 나을 것 같다. 
3. input 태그 세 개를 만들고 각각 tags, titles, texts 레이블 추가한다. 
```
      <div>
          <p></p>
          <form action="/@june0620/search/" method="get">
              <label >Tags: </label>
              <input id="tags" type="text" name="tags" value="">
              <label >Titles: </label>
              <input id="titles" type="text" name="titles" value="">
              <label >Texts: </label>
              <input id="texts" type="text" name="texts" value="">
              <input type="submit" value="Search">
          </form>       
      </div>
```
<p>

![](https://cdn.steemitimages.com/DQmaWkKn9ESkTtmMEDJWkTn3uZQE1zQD8kcndFCooB2u5rY/image.png)
<p>

대충 요런 화면인데 CSS가 없어서 이 모양이다. CSS 공부도 또 해야 하나? ㅠ 일단 기능에만 집중하자. 👇
![](https://cdn.steemitimages.com/DQmNY5tjh5TGr4Nn7zP7dWZ4JY3L34iNeJ6oBz4KJANHRcx/image.png)
##### URL 설정
form의 action으로 받은 url을 urls.py에 설정한다. 이 부분은 변수를 사용할 줄 아니 변수로 처리하자. ㅎㅎ 
url 패턴과 매칭 될 경우 views.py의 SearchPosts 클래스를 호출한다. 

```
    path('@<slug:account>/search/', SearchPosts.as_view(), name='search'),
```
<p>

![](https://cdn.steemitimages.com/DQmTFazmwMocShDKgQRAALGdC5ZhNkqJUckF89sp5EbvTsE/image.png)
##### form 파라미터 처리 및 검색
form 에서 전달받은 query string 을 리스트로 전환해야 하는데 split(',') 로 리스트화 및 query 딕셔너리에 추가한다. 
UI 단에서 동일 필드에 여러 개 검색어를 입력하고자 할 때 , 로 분리하면 더 많은 결과를 얻을 것이다. 
(일시적인 구현이고, 나중에 UI가 바뀌면 다시 구현 필요하다.)
```
class SearchPosts(ListView):
    template_name = 'album.html'
    context_object_name = 'all_posts'

    def get(self, request, *args, **kwargs):
        query = {
            'tags': request.GET.get('tags').split(','),
            'titles': request.GET.get('titles').split(','),
            'texts': request.GET.get('texts').split(',')
        }
        se = Search(account=kwargs['account'], query=query)
        self.queryset = se.search_posts()
        return super().get(request, *args, **kwargs)
```
<p>

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

##### 결과

![](https://cdn.steemitimages.com/DQmTMzFKPuTSGT6oSybGQiyzKaMe7nzfctm1VvrJX37b1wg/image.png)
<p>

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

**[Cookie 😅]**
Python 3.7.4
Django 2.2.4
steem-python 1.0.1
goorm IDE 1.3
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 5 others
properties (23)
authorjune0620
permlinkpython-17-django-10-ui
categoryhive-132971
json_metadata{"app":"peakd/2020.08.3","format":"markdown","tags":["kr","whalepower","mini","palnet","steempython","python","dblog","dev","django","goorm"],"users":["june0620"],"image":["https://cdn.pixabay.com/photo/2019/05/14/17/07/web-development-4202909_1280.png","https://cdn.steemitimages.com/DQmaWkKn9ESkTtmMEDJWkTn3uZQE1zQD8kcndFCooB2u5rY/image.png","https://cdn.steemitimages.com/DQmNY5tjh5TGr4Nn7zP7dWZ4JY3L34iNeJ6oBz4KJANHRcx/image.png","https://cdn.steemitimages.com/DQmTFazmwMocShDKgQRAALGdC5ZhNkqJUckF89sp5EbvTsE/image.png","https://cdn.steemitimages.com/DQmXwXF9SubesEuRy2as37uPtcpywdzM7WxSyKrzhxm4pwe/image.png","https://cdn.steemitimages.com/DQmTMzFKPuTSGT6oSybGQiyzKaMe7nzfctm1VvrJX37b1wg/image.png","https://cdn.steemitimages.com/DQmQKXV7ckRC6jg4LXYw14i5kJ7raK1VSGrJGF27Rp9Uhw4/image.png"]}
created2020-09-08 11:58:21
last_update2020-09-08 11:58:21
depth0
children1
last_payout2020-09-15 11:58:21
cashout_time1969-12-31 23:59:59
total_payout_value1.612 HBD
curator_payout_value1.436 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,563
author_reputation118,592,211,436,406
root_title"[Python #17] [Django #10] 검색 기능에 UI 추가"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id99,516,841
net_rshares11,456,982,585,026
author_curate_reward""
vote details (69)
@hivebuzz ·
Congratulations @june0620! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@june0620/upvotes.png?202009090039"></td><td>You distributed more than 20000 upvotes. Your next target is to reach 21000 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@june0620) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Do not miss the last post from @hivebuzz:**
<table><tr><td><a href="/hive-151781/@hivebuzz/meetup-uk"><img src="https://images.hive.blog/64x128/https://i.imgur.com/h6W1RRo.png"></a></td><td><a href="/hive-151781/@hivebuzz/meetup-uk">HiveBuzz supports meetups of the Hive UK Community</a></td></tr><tr><td><a href="/hivebuzz/@hivebuzz/feedback-from-the-september-1st-hive-power-up-day"><img src="https://images.hive.blog/64x128/https://i.imgur.com/zHjYI1k.jpg"></a></td><td><a href="/hivebuzz/@hivebuzz/feedback-from-the-september-1st-hive-power-up-day">Feedback from the September 1st Hive Power Up Day</a></td></tr></table>
properties (22)
authorhivebuzz
permlinkhivebuzz-notify-june0620-20200909t010632000z
categoryhive-132971
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2020-09-09 01:06:30
last_update2020-09-09 01:06:30
depth1
children0
last_payout2020-09-16 01:06:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,259
author_reputation370,010,458,624,423
root_title"[Python #17] [Django #10] 검색 기능에 UI 추가"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,527,160
net_rshares0