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
  • 668
  • 6
  • 20

1 Answers1

3

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,768
  • 3
  • 26
  • 34