[Python #18] [Django #11] form action 변수 적용 (부제: 타인 글 검색)
pixabay
지난 번 글에서 변수 처리할 줄 몰라 하드 코딩한 부분이 있다.
action은 일단 하드코딩으로 /@june0620/search로 한다. 계정명은 변수로 처리하고 싶은데 안된다. ㅠ (#연구필요#장고초짜)
내 계정으로 하드 코딩했기 때문에 다른 사람의 글은 검색이 안 되는 불편한 진실을 오늘 수정해본다. action 값에 계정명을 동적으로 넣어야 하는데 계정명을 입력받는 곳은 현재 URL밖에 없으니 URL에서 계정명만 추출해야 한다. 다행히 {{ request.path }}로 현재 접속 중인 URL을 가져올 수 있는 듯하다. Template filters로 URL에서 계정명만 추출하면 되겠다.
templatetags
templatetags 폴더 > post_extras.py에 필터를 하나 등록한다. URL을 인자로 받아 계정 추출하는 함수를 쓸 건데 정규 표현식을 동원해야 할 것 같은 느낌이 팍 온다. 아~~ 정규 표현식 모르는데... ㅠㅠ
인터넷에서 여러가지 방법 시도해 보다 가장 무난한 방법으로 가보자.
참고로 이런 정규식 체크 페이지도 있다.
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 상단에 필터를 load 하고, form action에 필터를 적용하면 되겠다.
{% 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 #kr 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 변수 적용 (부제: 타인 글 검색)