Marin
Marin

Reputation: 193

Why django class based View (DetailView) only works with a specific variable names

My following question is to understand why the variable template_name, for example with the class DetailView, only works with exactly that variable name.

For example in views.py:

    from django.views.generic import DetailView

        # /appPruebaDos/detailView/<int:pk>
        class DetailViewObject(DetailView):
            model = Articulo
            template_name = "plantillaDetailView.html" # varible fijado del tipo DetailView

Why I can't use other variable like(this don't work):

    # /appPruebaDos/detailView/<int:pk>
    class DetailViewObject(DetailView):
        model = Articulo
        templateName = "plantillaDetailView.html" # varible fijado del tipo DetailView

I think template_name field is defined in the DetailView dependencies, but how and where is defined?

Upvotes: 0

Views: 103

Answers (1)

Swetank Poddar
Swetank Poddar

Reputation: 1291

It is a good practise to follow the standard conventions of a language. In Python, the standard identifier naming convention is Snake Case (Example: template_name).

Secondly, the reason why templateName doesn't work and template_name does, is because the class you inherited (DetailView) looks for an attribute named template_name in your class.

You can see the way Django tries to find template_name in Django's Github repository. Here's the link to the base class of Django's views which implements a function called get_template_names().

Upvotes: 3

Related Questions