Django: How to reference a context variable created in one class-based view from another class-based view

Condado

In the simplified example below, how do I reference the context variable "Elephant" (created in the class Lion) from inside the class Leopard?

I have researched and tried a number of ways, including the last line below which results in a KeyError for "Elephant".

views.py

class Lion(ListView):
   
    def get_context_data(self, **kwargs):
        context super().get_context_data()
        context[“Elephant”] = Rhinoceros 
        context["Giraffe"] = Eagle
        return context

class Leopard(ListView):
    Buffalo = Lion.get_context_data(context[“Elephant”])
Code-Apprentice

One option is to create your own ListView super class:

class AnimalListView(ListView):
    def get_context_data(self, *args, **kwargs):
        super().get_context_data(*args, **kwargs)
        return {
            "Elephant": Rhinoceros,
            "Giraffe": Eagle,
        }

Now your other list views can inherit this instead of inheriting form ListView directly:

class LionList(AnimalListView):
    ...

class LeopartList(AnimalListView):
    ...

Alternatively, you can make the class a mixin:

class AnimalContextMixin:
    def get_context_data(self, *args, **kwargs):
        super().get_context_data(*args, **kwargs)
        return {
            "Elephant": Rhinoceros,
            "Giraffe": Eagle,
        }

And then your list views should inherit from both ListView and this mixin:

class LionList(AnimalContextMixin, ListView):
    ...

class LeopartList(AnimalContextMixin, ListView):
    ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django - Calling one class method from another in Class Based View

From Dev

Return context from class based view in django

From Dev

How do I return the result of a class-based view from another class-based view in Django?

From Dev

Django: Modify Class Based View Context (with **kwargs)

From Dev

Adding Context to Class Based View Django Project

From Dev

How to pass in the context of a Class Based View to the Template in Django?

From Dev

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

From Dev

Class based view Django

From Dev

How to send a context in a view based on class

From Dev

How to add a Context to a ListView Class Based View

From Dev

django print variable in template from class based view

From Dev

How to pass a variable between methods inside Django class based View

From Dev

Django: Extend context of class based view with Admin context

From Dev

how to turn class based view variable to function based view?

From Dev

How to convert a Function based view into a Class based View in django

From Dev

How to integrate a function based view into a class based view in Django?

From Dev

How to add/modify an attribute to a Django class based view from a Decorator?

From Dev

How to filter objects in a class based view django

From Dev

How to change template in Django class based view

From Dev

How to incorporate Reportlab with Django Class Based View?

From Dev

Context variables not being passed to template in Django class-based view

From Dev

Django Class based view get_context not finding get request

From Dev

Convert a function view to a Class Based View Django

From Dev

Setting initial formfield value from context data in Django class based view

From Dev

Send context the template in a class based view

From Dev

How to Add extra context for Class Based Login View?

From Dev

How to use context object to make query in Class based View?

From Dev

Django Class based view loading another form with data

From Dev

Django class based view, save in another model after CreateView

Related Related

  1. 1

    Django - Calling one class method from another in Class Based View

  2. 2

    Return context from class based view in django

  3. 3

    How do I return the result of a class-based view from another class-based view in Django?

  4. 4

    Django: Modify Class Based View Context (with **kwargs)

  5. 5

    Adding Context to Class Based View Django Project

  6. 6

    How to pass in the context of a Class Based View to the Template in Django?

  7. 7

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

  8. 8

    Class based view Django

  9. 9

    How to send a context in a view based on class

  10. 10

    How to add a Context to a ListView Class Based View

  11. 11

    django print variable in template from class based view

  12. 12

    How to pass a variable between methods inside Django class based View

  13. 13

    Django: Extend context of class based view with Admin context

  14. 14

    how to turn class based view variable to function based view?

  15. 15

    How to convert a Function based view into a Class based View in django

  16. 16

    How to integrate a function based view into a class based view in Django?

  17. 17

    How to add/modify an attribute to a Django class based view from a Decorator?

  18. 18

    How to filter objects in a class based view django

  19. 19

    How to change template in Django class based view

  20. 20

    How to incorporate Reportlab with Django Class Based View?

  21. 21

    Context variables not being passed to template in Django class-based view

  22. 22

    Django Class based view get_context not finding get request

  23. 23

    Convert a function view to a Class Based View Django

  24. 24

    Setting initial formfield value from context data in Django class based view

  25. 25

    Send context the template in a class based view

  26. 26

    How to Add extra context for Class Based Login View?

  27. 27

    How to use context object to make query in Class based View?

  28. 28

    Django Class based view loading another form with data

  29. 29

    Django class based view, save in another model after CreateView

HotTag

Archive