2

In Django template, How to get the slug of the URL I am on in the template so that I can pass it to the template tag.

Example I am on: foo.com/slugname

then in the template, I am expecting

{% function|slugname%}

where 'function' is my template tag.

Harsh Nagarkar
  • 697
  • 7
  • 23

1 Answers1

4

Assuming your urlpattern for the view is something like this:

urlpatterns = [
    path("<slug>", views.SomeView.as_view(), name="some_name"),
]

you can get the slug's value to use in your template using request.resolver_match.kwargs.slug.

See also Django documentation on the HttpRequest objects and the ResolverMatch class.

rafalmp
  • 3,988
  • 3
  • 28
  • 34