英文:
Django: How to reference a context variable created in one class-based view from another class-based view
问题
以下是翻译好的部分:
在下面的简化示例中,我如何从Leopard类内部引用在Lion类中创建的上下文变量"Elephant"?
我已经进行了研究并尝试了多种方法,包括下面的最后一行,但结果是"Elephant"的KeyError。
views.py
class Lion(ListView):
   
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["Elephant"] = Rhinoceros 
        context["Giraffe"] = Eagle
        return context
class Leopard(ListView):
    Buffalo = Lion.get_context_data(context["Elephant"])
英文:
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”])
答案1
得分: 2
有一些信息丢失,你到底想要实现什么,因为例子看起来不太合理 - 为什么要在Leopard中将Buffalo定义为类属性?
但一般来说,你可以在类外部定义项目,然后在两个视图中重复使用它们:
def get_animals():
    return {
        "Elephant": Rhinoceros,
        "Giraffe": Eagle,
    }
class Lion(ListView):
   
    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context.update(get_animals())
        return context
class Leopard(ListView):
    def get(self, *args, **kwargs):
        # 假设你不想将其作为属性
        animals = get_animals()
        Buffalo = Lion.get_context_data(animals["Elephant"])
英文:
There is missing some information what exactly are you trying to achieve as the example doesnt exactly make sense - why are you trying to define Buffalo as class property in Leopard?
But generally you can define the items somewhere outside the class and then reuse them in both views:
def get_animals():
    return {
        "Elephant": Rhinoceros,
        "Giraffe": Eagle,
    }
class Lion(ListView):
   
    def get_context_data(self, **kwargs):
        context = super().get_context_data()
        context.update(get_animals())
        return context
class Leopard(ListView):
    def get(self, *args, **kwargs):
        # assuming you dont want to have that as a property
        animals = get_animals()
        Buffalo = Lion.get_context_data(animals[“Elephant”])
答案2
得分: 1
以下是翻译好的代码部分:
创建自己的 `ListView` 超类的一种选项:
class AnimalListView(ListView):
    def get_context_data(self, *args, **kwargs):
        super().get_context_data(*args, **kwargs)
        return {
            "Elephant": Rhinoceros,
            "Giraffe": Eagle,
        }
现在,您的其他列表视图可以继承这个超类,而不是直接继承自 ListView:
class LionList(AnimalListView):
    ...
class LeopartList(AnimalListView):
    ...
或者,您可以将类制作为一个混合类:
class AnimalContextMixin:
    def get_context_data(self, *args, **kwargs):
        super().get_context_data(*args, **kwargs)
        return {
            "Elephant": Rhinoceros,
            "Giraffe": Eagle,
        }
然后,您的列表视图应该同时继承自 ListView 和这个混合类:
class LionList(AnimalContextMixin, ListView):
    ...
class LeopartList(AnimalContextMixin, ListView):
    ...
英文:
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):
    ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论