Django: displaying a form in a template with a prefix

I searched for an answer for a while without success. I basically want to get the name of the form field with the prefix enabled. Does anyone know how to do this?

Example:

Let's say I create a form in views.py and include a prefix like this:

myform = SomeForm(prefix="prefix") 

and then pass it to my template. If I then access the form field inside the template directly, I {{myform.username}} get something like this (note that the prefix has been enabled):

 <select id="id_prefix-username" name="prefix-username"> ... 

If I would like to manually create a form tag and just insert the name, then I will not get a prefix with it:

 <input name="{{ myform.username.name }}"> 

will generate:

 <input name="username"> 

Notice how the prefix is ​​now missing. Now I can do it this way to achieve the same result:

  <input name="{{ myform.prefix }}-{{ myform.username.name }}"> 

But should there be an inline way to do this?

+4
source share
1 answer

You can use html_name instead:

 <input name="{{ myform.username.html_name }}"> 

This should add a prefix as needed.

+9
source

Source: https://habr.com/ru/post/1495925/


All Articles