Since I manully create a lot of templates for fields and try to re-use templates I would like to know how to change the name of the form (used in template) in a class based view.
Are you looking to change the name of the form as it is used in the templates, or change the form class being used? (Two different answers.)
If you’re looking to share/reuse templates for similar-but-not-identical situations, you’re probably best off using generic names for the form(s) in the template.
I am looking at changing the the first… and atm specifically for forms.
I can do that in the function based views as seen in the code but I cannot find how to in the class based views.
I want to use the same templates in both function based views and class based views for forms. the above code is the solution for function based views but if I use
{{ form_1 }}
I want to rename the form in class based views to form_1 too
What are your actual objectives here? What are the benefits that you are hoping to obtain?
It might be helpful if you posted the CBV that you’re trying to alter the behavior of, and identify specifically what your end goal is.
I know you’re trying to address this in general terms, but there’s still a lot of ambiguity regarding the desired results.
In general terms, you’re frequently better off adapting your templates to work with the CBVs rather than the reverse, but it’s not a requirement.
Specifically, regarding this:
What “name” are you referring to?
If you’re not familiar with the Django CBV internals, I recommend becoming familiar with the Classy Class-Based Views site, specifically, the FormView to get an understanding of what attributes, variables, and functions are used by that view.
By default, an UpdateView expects the instance of the form to be rendered to be present in the context by the name “form”. That’s the easiest way to handle this. Have your template render {{ form }}, and the rest works directly.
-or-
Override the get_context_data function to assign the form instance to the name being used in the template.
e.g.:
My objective is to re-use as mutch code as possible.
I will try to show the process that resulted in me wanting to change the form name in class based views:
I had problems showing some fields so I changed all my forms to manually created parts per model field. For instance my person form template consists of:
If the section of the template that includes these lines:
Is what’s in one of the include directives, perhaps:
Then keep in mind that you can assign a name to a context reference in the include statement with the with directive. See include.
This means you can do something like: {% include "MyNetwork/form/person/full_name_fields_form.html" with form=form_1 %}
and {% include "MyNetwork/form/person/full_name_fields_form.html" with form=form_2 %}
where references to form within the full_name_fields_form.html template are referring to the context entries form_1 and form_2 respectively.
The templating system is very much designed around being flexible and used in a “generic” manner. Look at the system provided templates for things like the admin and the standard widgets to get some ideas as to just how flexible they can be.
And so the reason I recommend going with option 1 above is that learning how to create and use these types of generic templates goes a long way toward reducing duplicated functionality.
Now, having said all this, I will point out that the Django-provided CBVs are not a good solution for “multiple-form” views. Yes, they can be adapted to handling multiple forms, but it’s not really what they’re designed to do and you end up having to override more functionality than you might like to ensure that both forms are handled properly.
If you have multiple views where each view is having to properly manage multiple forms, my suggestion is to either use FBVs - or if you have enough of these where it’s worthwhile to do so, create your own CBVs starting from View.
I dont see how I can do that but I will start a few topics soon to see if / how/if there are better solutions for my problems. I have a fewproblems I solved by creating manual templates and a lot of extra code. I will see if that can be done easyer. That said… you gave me the solution I asked for and a lot to think about. thanks for all the help.