简体   繁体   English

模板的Django上下文变量名称

[英]Django context variable names for the templates

EDIT: I know I can change the names of the variables. 编辑:我知道我可以更改变量的名称。 My question is in the case that I don't want to do that. 我的问题是我不想这样做。 I want to know what are all the variables that django generates automatically. 我想知道django自动生成的所有变量是什么。


I'm doing Django's getting started tutorial and I'm on the generic views section where at one point it explains: 我正在做Django的入门教程,并且在“ 通用视图”部分中,它在某一点上解释了:

In previous parts of the tutorial, the templates have been provided with a context that contains the question and latest_question_list context variables. 在本教程的前面部分中,已为模板提供了一个上下文,该上下文包含问题和latest_question_list上下文变量。 For DetailView the question variable is provided automatically – since we're using a Django model (Question), Django is able to determine an appropriate name for the context variable. 对于DetailView,将自动提供问题变量–由于我们使用的是Django模型(问题),因此Django能够为上下文变量确定适当的名称。 However, for ListView, the automatically generated context variable is question_list. 但是,对于ListView,自动生成的上下文变量是question_list。

My problem is that I don't know how Django determines this "appropriate names". 我的问题是我不知道Django如何确定这个“适当的名称”。 I want to know this for when I write my own template. 当我编写自己的模板时,我想知道这一点。 I would want to know what context variable names to use in such template. 我想知道在此类模板中使用什么上下文变量名称。

From what I can understand, if my model is Question , the question context variable would store that question, and the question_list context variable would store every question. 据我了解,如果我的模型是Question ,那么question上下文变量将存储该问题,而question_list上下文变量将存储每个问题。

So my doubt is: What other context variables names can I use? 所以我的疑问是:我还可以使用其他哪些上下文变量名称? and what would they store? 他们会存储什么? I can't seem to find this on the documentation, please redirect me to it if you know where it is. 我似乎在文档中找不到此文件,如果您知道它在哪里,请重定向至它。

I think this default context variable name only applies when dealing with Django's Class Based Views. 我认为此默认上下文变量名称仅在处理Django的基于类的视图时适用。

Eg If you are using a DetailView for a Animal model, Django will auto create a context variable called 'animal' for you to use in template. 例如,如果将DetailView用于Animal模型,则Django将自动创建一个名为“ animal”的上下文变量供您在模板中使用。 I think it also allows the use of 'object'. 我认为它也允许使用“对象”。

Another example is, as you mentioned, the ListView for a Animal model which would generate context name called animal_list. 如您所提到的,另一个示例是Animal模型的ListView,它将生成名为animal_list的上下文名称。

However, in both of these cases, there are ways to change the default context variable name. 但是,在这两种情况下,都有一些方法可以更改默认上下文变量名称。 If you specify 'context_object_name' in your DetailView, this will be the name you refer to in your template. 如果在DetailView中指定“ context_object_name”,则这将是您在模板中引用的名称。 This will also work for ListViews. 这也适用于ListViews。

This website has all info on CBVs of all Django versions: 该网站包含所有Django版本的CBV的所有信息:

https://ccbv.co.uk/projects/Django/1.9/django.views.generic.detail/DetailView/ https://ccbv.co.uk/projects/Django/1.9/django.views.generic.detail/Det​​ailView/

You can change the question_list to something else by using the context_object_name this isn't explained all that well in that part of the documentation, but ... 您可以使用context_object_name来将question_list更改为其他名称,这在本文档的那一部分中并不能很好地解释,但是...

Return the context variable name that will be used to contain the list of data that this view is manipulating. 返回上下文变量名称,该名称将用于包含此视图正在处理的数据列表。 If object_list is a queryset of Django objects and context_object_name is not set, the context name will be the model_name of the model that the queryset is composed from, with postfix '_list' appended. 如果object_list是Django对象的查询集,并且未设置context_object_name,则上下文名称将是组成查询集的模型的model_name,并附加后缀'_list'。 For example, the model Article would have a context object named article_list. 例如,模型Article将具有一个名为article_list的上下文对象。

is given under get_context_object_name method get_context_object_name方法下给出

This is what that method's code looks like, It ought to clear up all doubts: 该方法的代码如下所示:应该清除所有疑问:

    """
    Get the name of the item to be used in the context.
    """
    if self.context_object_name:
        return self.context_object_name
    elif hasattr(object_list, 'model'):
        return '%s_list' % object_list.model._meta.model_name
    else:
        return None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM