www.example.com/slug/PrimaryKey
www.example.com/foo/1
如果是这样,我该如何传递变量。
例如
{% 'example:posts' slug='slug' pk=1 %} # would this work?
答案 0 :(得分:2)
urls.py:
# Django 2.0
urlpatterns = [
path('<slug:slug>/<int:pk>', views.posts, name='posts'),
]
# Django 1.11 and bellow:
urlpatterns = [
url('(?P<slug>[-\w]+)/(?P<pk>\d+)/$', views.posts, name='posts'),
]
模板:
{% url 'posts' 'slug' 1 %}
如果您将“ post”对象传递到模板,我认为这是您的情况,但我不确定:
{% url 'posts' post.slug post.pk %}