0
votes

Is there a way to set the 'id' attribute in the template? let's say i pass my form as context in my view so when I render some field from my form like {{form.field1}} can I set the 'id' something like {{form.fiel1|attrib=('id':'id_forfieldone')}}

I know that can be done in the form class definition, but I need to do it in the template.

EDIT: Here is the error

Traceback (most recent call last): File "manage.py", line 17, in execute_from_command_line(sys.argv) File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 351, in execute_from_command_line utility.execute() File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 343, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 177, in fetch_command commands = get_commands() File "C:\Python27\lib\site-packages\django\utils\lru_cache.py", line 101, in wrapper result = user_function(*args, **kwds) File "C:\Python27\lib\site-packages\django\core\management__init__.py", line 72, in get_commands for app_config in reversed(list(apps.get_app_configs())): File "C:\Python27\lib\site-packages\django\apps\registry.py", line 137, in get_app_configs self.check_apps_ready() File "C:\Python27\lib\site-packages\django\apps\registry.py", line 124, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

1
The id of a form field is id_fieldname by default, so in your case it would be id_field1. Why do you need to set it to something else?John Gordon
because I add fields dinamically and need to set diferent id for each one.M. Gar

1 Answers

1
votes

You can use Widget Tweaks. With help of this you can add classes to your input and edit other attribute also. In your case to edit id attribute you can use it like -

{% render_field form.fiel1|attr:"id:id_forfieldone" %}

Here is good tutorial also.