0

Starting from a Class based update view (Header table). I call a Class based List View to display all the details lines created (Lines table) for my Header. I have created a GLOBAL val that contains the key value of my Header. Everything was working fine since I tried to insert a button to go back to my Header. The URL works perfectly if I hardcode the key value of my Header. If I try to insert my Global variable it doesn't work. I tried with val(), pk=val, pk=val() but the url doesn't work. It seems that the variable is empty... Most of the time test crash with Error message saying that Reverse for 'headfttlog_update' with keyword arguments '{'pk': ''}' not found. Could someone help me? Thanks in advance.

Urls.py:

urlpatterns = [
re_path(r'^headfttlog/update/(?P<pk>[-\w0-9_@]+)/$',
HeadfttlogUpdateView.as_view(), name='headfttlog_update'),
]

Views.py: in DEBUG I'm printing the val and the content is OK!

class DeafttlogListView(LoginRequiredMixin, PermissionRequiredMixin, 
      ListView):
    permission_required = 'fttlapp.view_deafttlog'

    model = Deafttlog
    paginate_by = 10

    def get_queryset(self):
        queryset = Deafttlog.objects.filter(dea_fttlcode=val())
        print('Uscita Queryset Lista Deafttlog' )
        print(val())
        return queryset

html:

<a class="btn btn-outline-info" href="{% url 'headfttlog_update' val %}"
>Back to FTTL header</a>
deepdiver
  • 15
  • 4
  • Does this answer your question? [How to add data to context object in DetailView?](https://stackoverflow.com/questions/50596961/how-to-add-data-to-context-object-in-detailview) – Abdul Aziz Barkat Sep 30 '21 at 15:59
  • 1
    1) Whatever data you want to use in the template you need to pass in the context (The template doesn't care about the global variables, view's local variables, etc.). 2) **Don't** use global variables to store data in Django unless you _really really know_ what you are doing. – Abdul Aziz Barkat Sep 30 '21 at 16:01
  • @AbdulAzizBarkat I tried to do that before posting my question but I was not successfull in my attempt. I have to transmit my value from one view to another view. Header update view to lines list view and then turn back to the Header update view. I need to store and transmit the Header key to all the views. I'm surely doing something wrong but I don't understand what. Thanks for your advice on Global value, I used it because it's the only way I found to keep stored and transmit my key. If there is another way to do it I will follow it. – deepdiver Oct 01 '21 at 14:27
  • 1
    Each request should be _independent_ of each other, any data it needs should be fetched from some storage (mostly the database), global variables are a **bad** solution to do what you want (What happens when multiple people request that page? When there are multiple servers?) – Abdul Aziz Barkat Oct 01 '21 at 15:29
  • @AbdulAzizBarkat implemented today a cache management. Thanks for your advice – deepdiver Oct 03 '21 at 18:59

0 Answers0