简体   繁体   中英

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.


I'm doing Django's getting started tutorial and I'm on the generic views section where at one point it explains:

In previous parts of the tutorial, the templates have been provided with a context that contains the question and latest_question_list context variables. 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. However, for ListView, the automatically generated context variable is question_list.

My problem is that I don't know how Django determines this "appropriate names". 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.

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.

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. 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.

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. This will also work for ListViews.

This website has all info on CBVs of all Django versions:

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

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 ...

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. For example, the model Article would have a context object named article_list.

is given under get_context_object_name method

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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