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

pixabay

<p dir="auto">在<a href="https://steemit.com/hive-101145/@june0620/3sz4dp-python-17-django-10-ui" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">上回,有一个地方我用硬编码,因我不会用变量。 <blockquote> <p dir="auto">action 值先用硬编码 /@june0620/search赋值,账号应该用变量,但目前还不知道怎么用变量。 没办法,谁让我是django初学者呢。 <p dir="auto">当时用我的账号硬编码,所以无法搜索他人的文章。今天解决这个问题吧。<br /> 从URL获取账号赋值给action参数。幸好Django提供获取URL的方法<code>{{ request.path }},通过Template filters创建一个过滤器应该可以过滤账号了。 <h5>templatetags <p dir="auto">在 templatetags 文件夹 > post_extras.py文件里注册一个过滤器。过滤的方法用正规表达式最好不过,但我不太会用正规表达式。哎~没事儿,学吧。 <p dir="auto"><span>经过我精心的搜索,搜出了一个非常好用的正规表达式检查器。<a href="https://regex101.com/r/cO8lqs/4" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://regex101.com/r/cO8lqs/4 就用你了~ <pre><code>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 dir="auto"> <p dir="auto"><img src="https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmdCAPzAmj4PnJLtWxLSTpgxbU1ZystDbc2E1PYcD6MNkS/image.png" srcset="https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmdCAPzAmj4PnJLtWxLSTpgxbU1ZystDbc2E1PYcD6MNkS/image.png 1x, https://images.hive.blog/1536x0/https://cdn.steemitimages.com/DQmdCAPzAmj4PnJLtWxLSTpgxbU1ZystDbc2E1PYcD6MNkS/image.png 2x" /> <h5>base.html <p dir="auto">在 base.html 载入过滤器, form action 也改用过滤器就OK了。 <pre><code>{% 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 dir="auto"> <p dir="auto"><img src="https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmcQzKTGkiSocuYPrwYPJ3KXvc92ZtnxYWsRANkzK5mc7k/image.png" srcset="https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmcQzKTGkiSocuYPrwYPJ3KXvc92ZtnxYWsRANkzK5mc7k/image.png 1x, https://images.hive.blog/1536x0/https://cdn.steemitimages.com/DQmcQzKTGkiSocuYPrwYPJ3KXvc92ZtnxYWsRANkzK5mc7k/image.png 2x" /> <h5>结果 <p dir="auto"><br /> <img src="https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmQrQ5RoXTnA5piqTsEUWQd3P9o6w7HJwpxthU9aoBccb7/image.png" srcset="https://images.hive.blog/768x0/https://cdn.steemitimages.com/DQmQrQ5RoXTnA5piqTsEUWQd3P9o6w7HJwpxthU9aoBccb7/image.png 1x, https://images.hive.blog/1536x0/https://cdn.steemitimages.com/DQmQrQ5RoXTnA5piqTsEUWQd3P9o6w7HJwpxthU9aoBccb7/image.png 2x" /><span>哈哈,现在可以搜索萍萍(<a href="/@annepink">@annepink)的熊孩纸文章了。😄 <hr /> <p dir="auto"><strong>[Cookie 😅]<br /> Python 3.7.4<br /> Django 2.2.4<br /> steem-python 1.0.1<br /> goorm IDE 1.3 <p dir="auto">参考文章:<br /><span> <a href="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" target="_blank" rel="nofollow noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">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 <p><p>
Sort:  

Hi 🙋🏻‍♀️

@tipu curate

Upvoted 👌 (Mana: 18/45)

晚上好,萍萍~ 😄
谢谢tipu

这么巧 😳难道我收到你艾特我了😂😅😁厉害了俊😎

实现完搜索功能,尝试搜索你的文章,很成功~ 😃

🥰✌🏻

I have picked your post for my daily hive voting initiative, Keep it up and Hive On!!