0
 path('clock/view/<user_id>', views.ClockInOutViewListView.as_view(), name='admin_timesheet_clock_in_out_view'),

I am passing user_id in url and looks like this -

http://localhost:8000/timesheet/clock/view/13

I want that user_id in my template to reuse that user_id Example - 13.

I was trying to {{user_id}} in my template but it did not got any value

How can i get that 13 value in my template which is passed in my url

class ClockInOutViewListView(LoginRequiredMixin, generic.TemplateView):
    template_name = 'clock/clock-view.html'

    def get_context_data(self, user_id, **kwargs):   
        context = super(ClockInOutViewListView, self).get_context_data(**kwargs)
        try:
            context['views'] = TimesheetEntry.objects.filter(
                                    timesheet_users = user_id,
                                ).order_by('-id')
        except: pass

        return context
Cipher
  • 2,060
  • 3
  • 30
  • 58

1 Answers1

1

You can access the user_id by self.kwargs['user_id'] where the self is the view instance

try this,

class ClockInOutViewListView(LoginRequiredMixin, generic.TemplateView):
    template_name = 'clock/clock-view.html'

    def get_context_data(self, **kwargs):
        user_id = self.kwargs['user_id']
        context = super(ClockInOutViewListView, self).get_context_data(**kwargs)
        context['user_id'] = user_id
        return context


Now, you can access the user_id in your template as {{ user_id }}


Apart from that, I would suggest to avoide bare try..except pass block, because it's bad behavoiur Read more here .. Why is “except: pass” a bad programming practice?

JPG
  • 82,442
  • 19
  • 127
  • 206