Home > Net >  How to save url <str:> part in Django Class Based View context
How to save url <str:> part in Django Class Based View context

Time:08-05

I am struggling to get the value uuid_contrat in my url into my views' context. This is what I have got so far:

urls.py

from django.contrib import admin
from django.urls import path, include
from applications.views import *
from . import views

urlpatterns = [
    path('<str:uuid_contrat>/', ApplicationListView.as_view(), name="applications") ,
]

views.py

class ApplicationListView(ListView):
    model = ViewDevpermissionapplicationuser
    template_name = 'applications/applications.html'

    def get_context_data(self, *args, **kwargs):
        kwargs.update(
            user= self.request.session.get('user', {'is_authenticated' : False})
        )
        
       
        context['uuid_contrat'] = self.request.GET.get('uuid_contrat')
        
        return  context

I tried using request.GET.get but it isn't working, any suggestions ?

CodePudding user response:

You can use self.kwargs['uuid_contrat'] to fetch the uuid from the url kwargs, so:


class ApplicationListView(ListView):
    model = ViewDevpermissionapplicationuser
    template_name = 'applications/applications.html'

    def get_context_data(self, *args, **kwargs):
        kwargs.update(
            user=self.request.session.get('user', {'is_authenticated': False})
        )

        context['uuid_contrat'] = self.kwargs['uuid_contrat']

        return context
  • Related