[Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)
pixabay
在上回,有一个地方我用硬编码,因我不会用变量。
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
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>
结果
[Cookie 😅]
Python 3.7.4
Django 2.2.4
steem-python 1.0.1
goorm IDE 1.3
Leave [Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索) to:
Read more #cn posts
Best Posts From ETHAN
We have not curated any of june0620's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From ETHAN
- [Life] 献血 #7
- [Life] 산과 바다를 동시에 느끼려면!!! 통영!!! 클럽이에스 리조트!!! #1
- [Life] 韩国的驾照有意思~
- [Coin] 从NEXO发送coin到Bitfinex需要谨慎!
- [Coin] 好久不见!QTUM
- My Actifit Report Card: 4월 18 2021
- [Coin] 现在的市场太激烈,找个稳定币稳定稳定
- My Actifit Report Card: 4월 4 2021
- [Coin] ETH2 锁仓70日收益
- [Life] 韩国一部分女性要求男人坐小便,怎么想?
- [it] Chrome os 88 添加性能监控
- [Life] Happy 牛 Year
- [IT] Chromium取Web代App
- [IT] SAMSUNG Galaxy Chromebook #6 在 Chromebook Linux安装 Postman
- [JAVA #23] [Troubleshooting #5] TestNG 运行结果在Eclipse显示乱码
- [Life] blood donation #3
- [Python #19] [Django #12] 穿上CSS搜索功能
- [Python #19] [Django #12] 검색 기능 UI 다듬기
- [Python #18] [Django #11] form action 添加变量(副题:搜索他人搜索)
- [Python #18] [Django #11] form action 변수 적용 (부제: 타인 글 검색)