[Solved]-Change the name attribute of form field in django template using

5👍

form.fields['new_name'] = form.fields['item']
del form.fields['item']

5👍

I’ve tested this a few different ways, and it works with many types of Form Fields.

Use set_field_html_name(...) on every Field you want to set the name on.

from django import forms
from django.core.exceptions import ValidationError

def set_field_html_name(cls, new_name):
    """
    This creates wrapper around the normal widget rendering, 
    allowing for a custom field name (new_name).
    """
    old_render = cls.widget.render
    def _widget_render_wrapper(name, value, attrs=None):
        return old_render(new_name, value, attrs)

    cls.widget.render = _widget_render_wrapper

class MyForm(forms.Form):
    field1 = forms.CharField()
    # After creating the field, call the wrapper with your new field name.
    set_field_html_name(field1, 'new_name')

    def clean_field1(self):
        # The form field will be submit with the new name (instead of the name "field1").
        data = self.data['new_name']
        if data:
            raise ValidationError('Missing input')
        return data

4👍

class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.fields['field_name'].label = "New Field name"

1👍

from django.forms.widgets import Input, TextInput


class CustomInput(Input):
    def get_context(self, name, value, attrs):
        context = super(CustomInput, self).get_context(name, value, attrs)
            if context['widget']['attrs'].get('name') is not None:
                context['widget']['name'] = context['widget']['attrs']['name']
        return context


class CustomTextInput(TextInput, CustomInput):
    pass


class ClientLoginForm(forms.Form):

    username = forms.CharField(label='CustomLabel', widget=CustomTextInput(attrs={'class': 'form-control','name': 'CustomName'}))

0👍

You can subclass any widget class what you need and create your own “render method”.
Examples are in the PATH_TO_YOUR_DJANGO/django/forms/forms.py

class CustomNameTextInput(TextInput):
    def render(self, name, value, attrs=None):
        if 'name' in attrs:
            name = attrs['name']
            del attrs['name']
        return super(TextInput, self).render(name, value, attrs)


class MyForm(Form):
    item = CharField(widget=CustomNameTextInput, attrs={'name':'my_name'})

0👍

Another way, creating a custom input that accepts a new parameter called "name" [I also created a custom field that uses this input]:

class CustomNameTextInput(forms.TextInput):
    def __init__(self, *args, **kwargs):
        self.name = kwargs.pop('name')
        super().__init__(*args, **kwargs)

    def render(self, name, value, attrs, renderer):
        return super().render(self.name, value, attrs, renderer)


class ElementField(forms.CharField):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.widget = CustomNameTextInput(name='name')
👤j4n7

0👍

You can try this :

def set_field_html_name(cls, new_name):
    """
    This creates wrapper around the normal widget rendering, 
    allowing for a custom field name (new_name).
    """
    old_render = cls.widget.render
    def widget_render_wrapper(name, value, attrs=None,renderer=None):
        return old_render(new_name, value, attrs,renderer=None)

    cls.widget.render = widget_render_wrapper

Leave a comment