【发布时间】:2020-08-22 21:59:15
【问题描述】:
如果我想将博文 id 发送到 url 模式,并让 url 自动变成 www.blog.com/post/2/this-is-my-second-blogpost 之类的东西,我该怎么做?
在 urls.py 内部,我有一个 url 模式,它接受博文 ID 及其标题。它似乎不起作用,一旦起作用,以这种方式为每个页面添加标题会很乏味。
urls.py:path('post/<int:post_id>/<str:post_title>', views.view_post, name='view_post'),
blogposts.html 模板:
<a href="{% url 'blogs:view_post' post.id post.title%}">Read More</a>
views.py:def view_post(request, post_id, post_title):.
【问题讨论】:
-
网址中不需要标题。目的是什么?
-
所以如果链接是共享的,用户可以在点击之前阅读标题
标签: django url django-urls