create account

[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索) by june0620

View this thread on: hive.blogpeakd.comecency.com
· @june0620 ·
$2.57
[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)
pixabay
https://cdn.pixabay.com/photo/2019/03/12/20/27/kanban-4051777_1280.jpg

在[上回](https://steemit.com/hive-101145/@june0620/3sz4dp-python-17-django-10-ui),有一个地方我用硬编码,因我不会用变量。
> action 值先用硬编码 /@june0620/search赋值,账号应该用变量,但目前还不知道怎么用变量。 没办法,谁让我是django初学者呢。

当时用我的账号硬编码,所以无法搜索他人的文章。今天解决这个问题吧。
从URL获取账号赋值给action参数。幸好Django提供获取URL的方法`{{ request.path }}`,通过Template filters创建一个过滤器应该可以过滤账号了。

##### templatetags 
在 templatetags 文件夹 > post_extras.py文件里注册一个过滤器。过滤的方法用正规表达式最好不过,但我不太会用正规表达式。哎~没事儿,学吧。

经过我精心的搜索,搜出了一个非常好用的正规表达式检查器。https://regex101.com/r/cO8lqs/4 就用你了~
```
import re
...
...

@register.filter
def get_account_form_url(path: str):
    account = re.search(r'@[a-z0-9-.]*', path)
    print(path)
    print(account.group(0))
    return account.group(0) if account else None
```
<p>

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

##### base.html
在 base.html 载入过滤器, form action 也改用过滤器就OK了。 

```
{% load post_extras %}
...
...

          <form action="/{{ request.path | get_account_form_url }}/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>  
```
<p>

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

##### 结果
 
哈哈,现在可以搜索萍萍(@annepink)的熊孩纸文章了。😄
![](https://cdn.steemitimages.com/DQmQrQ5RoXTnA5piqTsEUWQd3P9o6w7HJwpxthU9aoBccb7/image.png)

***
**[Cookie 😅]**
Python 3.7.4
Django 2.2.4
steem-python 1.0.1
goorm IDE 1.3

参考文章:
https://medium.com/@chrisjune_13837/%EC%A0%95%EA%B7%9C%EC%8B%9D-%ED%8A%9C%ED%86%A0%EB%A6%AC%EC%96%BC-%EC%98%88%EC%A0%9C%EB%A5%BC-%ED%86%B5%ED%95%9C-cheatsheet-%EB%B2%88%EC%97%AD-61c3099cdca8
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 13 others
properties (23)
authorjune0620
permlinkpython-18-django-11-form-action-qgjli6
categoryhive-105017
json_metadata{"app":"peakd/2020.09.4","format":"markdown","tags":["cn","whalepower","palnet","python","dblog","dev","django","goorm"],"users":["june0620","register.filter","annepink","chrisjune"],"links":["https://steemit.com/hive-101145/@june0620/3sz4dp-python-17-django-10-ui","https://regex101.com/r/cO8lqs/4","/@annepink","https://medium.com/@chrisjune_13837/%EC%A0%95%EA%B7%9C%EC%8B%9D-%ED%8A%9C%ED%86%A0%EB%A6%AC%EC%96%BC-%EC%98%88%EC%A0%9C%EB%A5%BC-%ED%86%B5%ED%95%9C-cheatsheet-%EB%B2%88%EC%97%AD-61c3099cdca8"],"image":["https://cdn.pixabay.com/photo/2019/03/12/20/27/kanban-4051777_1280.jpg","https://cdn.steemitimages.com/DQmdCAPzAmj4PnJLtWxLSTpgxbU1ZystDbc2E1PYcD6MNkS/image.png","https://cdn.steemitimages.com/DQmcQzKTGkiSocuYPrwYPJ3KXvc92ZtnxYWsRANkzK5mc7k/image.png","https://cdn.steemitimages.com/DQmQrQ5RoXTnA5piqTsEUWQd3P9o6w7HJwpxthU9aoBccb7/image.png"]}
created2020-09-12 11:06:12
last_update2020-09-12 11:06:12
depth0
children7
last_payout2020-09-19 11:06:12
cashout_time1969-12-31 23:59:59
total_payout_value1.364 HBD
curator_payout_value1.202 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,967
author_reputation118,592,211,436,406
root_title"[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id99,586,684
net_rshares10,575,557,741,566
author_curate_reward""
vote details (77)
@annepink ·
Hi 🙋🏻‍♀️

@tipu curate
properties (22)
authorannepink
permlinkqgjlp4
categoryhive-105017
json_metadata{"users":["tipu"],"app":"hiveblog/0.1"}
created2020-09-12 11:10:18
last_update2020-09-12 11:10:18
depth1
children2
last_payout2020-09-19 11:10:18
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_length22
author_reputation1,025,803,312,269,923
root_title"[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,586,725
net_rshares0
@june0620 ·
晚上好,萍萍~ 😄
谢谢tipu
properties (22)
authorjune0620
permlinkre-annepink-qgjlve
categoryhive-105017
json_metadata{"tags":["hive-105017"],"app":"peakd/2020.09.4"}
created2020-09-12 11:14:03
last_update2020-09-12 11:14:03
depth2
children0
last_payout2020-09-19 11:14:03
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_length16
author_reputation118,592,211,436,406
root_title"[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,586,747
net_rshares0
@tipu ·
<a href="https://tipu.online/hive_curator?annepink" target="_blank">Upvoted  &#128076;</a> (Mana: 18/45)
properties (22)
authortipu
permlinkre-qgjlp4-20200912t111028
categoryhive-105017
json_metadata""
created2020-09-12 11:10:30
last_update2020-09-12 11:10:30
depth2
children0
last_payout2020-09-19 11:10: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_length104
author_reputation55,900,354,328,870
root_title"[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,586,727
net_rshares0
@annepink · (edited)
这么巧 😳难道我收到你艾特我了😂😅😁厉害了俊😎
properties (22)
authorannepink
permlinkqgjlx7
categoryhive-105017
json_metadata{"app":"hiveblog/0.1"}
created2020-09-12 11:15:09
last_update2020-09-12 11:16:33
depth1
children2
last_payout2020-09-19 11:15:09
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_length23
author_reputation1,025,803,312,269,923
root_title"[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,586,758
net_rshares0
@june0620 ·
实现完搜索功能,尝试搜索你的文章,很成功~ 😃
properties (22)
authorjune0620
permlinkre-annepink-qgjq51
categoryhive-105017
json_metadata{"tags":["hive-105017"],"app":"peakd/2020.09.4"}
created2020-09-12 12:46:21
last_update2020-09-12 12:46:21
depth2
children1
last_payout2020-09-19 12:46:21
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_length23
author_reputation118,592,211,436,406
root_title"[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,587,650
net_rshares0
@annepink · (edited)
🥰✌🏻
properties (22)
authorannepink
permlinkqgjr4t
categoryhive-105017
json_metadata{"app":"hiveblog/0.1"}
created2020-09-12 13:07:45
last_update2020-09-12 13:08:06
depth3
children0
last_payout2020-09-19 13:07:45
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_length3
author_reputation1,025,803,312,269,923
root_title"[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,587,921
net_rshares0
@chitty ·
I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!
properties (22)
authorchitty
permlinkre-python-18-django-11-form-action-qgjli6-20200913t000227
categoryhive-105017
json_metadata""
created2020-09-13 00:02:30
last_update2020-09-13 00:02:30
depth1
children0
last_payout2020-09-20 00:02: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_length86
author_reputation86,901,300,608,582
root_title"[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,596,270
net_rshares0