Access kwargs from a URL in a Django template

后端 未结 1 1193
夕颜
夕颜 2020-12-01 14:49

Can I access value of a named argument (from the URL) in a Django template?

Like can I access the value of this_name below from a django template?

相关标签:
1条回答
  • 2020-12-01 15:16

    In the view, you can access the URL args and kwargs as self.args and self.kwargs.

    class MyView(View):
        def my_method(self):
            this_name = self.kwargs['this_name']
    

    If you only want to access the value in the template, then you don't need to make any changes in the view. The base get_context_data method adds the view to the context as view, so you can add the following to your template:

    {{ view.kwargs.this_name }}
    
    0 讨论(0)
提交回复
热议问题