Asif
Asif

Reputation: 1825

Change the name attribute of form field in django template using

I have form field {{form.item}} which will render to

        <input type="text" name="item" >

How can i change the name attribute of the form field using custom template tags?

I tried by sending the form to template tag where

       form.fields['item'].widget.attrs['name'] = 'new_name'

But I'm not getting success.

I need to change the name attribute in template.

UPDATE

models.py

class A(models.Model):
     name = models.CharField(50)
     type = models.CharField(50)

class B(models.Model):
     field1 = ForeignKeyField(A)
     value = IntegerField()

views.py

 def saving_calculation(request):

    SavingFormset = modelformset_factory(A, extra=2)
    OfferInlineFormSet = inlineformset_factory(
                     A, B,
                     extra = 4
                     )

   if request.method == 'POST':
      pass
   else:
       offer_formset = OfferInlineFormSet()
       saving_formset = SavingFormset(queryset=SavingCalculation.objects.none()) 

   return render_to_response(
       'purchasing/saving_calculation.html',
       {
       'offer_formset':offer_formset,
       'saving_formset':saving_formset,
       }

template

  <form action="." method="POST">
  {{ offer_formset.management_form }}
  {{ saving_formset.management_form }}
  {{ saving_formset.prefix }}
  <table>
 <thead>
    <tr>
        <th>Business Unit</th>
    <th>Category</th>
    <th>Buyer</th>
    <th>Offer1</th>
    <th>Offer2</th>
    <th>Offer3</th>
    <th>Offer4</th>
    </tr>
     </thead>
 <tbody>
        {% for saving in saving_formset.forms %}
     <tr>
    <td>{{saving.businessunit}}</td>
    <td>{{saving.type_of_purchase}}</td>
    <td>{{saving.buyer}}</td>
    {% for offer in offer_formset.forms %}
        <td>{{ offer|set_field_attr:forloop.counter0 }}</td>
    </tr>
        {% endfor %}

     {% endfor %}

      </tbody>
     </table>
     <input type="submit" value="Save" />
    </form>

Now in custom template tag i need to assign new name for each field of inline formset

Upvotes: 11

Views: 35014

Answers (7)

user20767128
user20767128

Reputation: 1

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

Upvotes: 0

j4n7
j4n7

Reputation: 608

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')

Upvotes: 0

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'}))

Upvotes: 1

HelpyHelperton
HelpyHelperton

Reputation: 1855

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

Upvotes: 6

Alessandro Odetti
Alessandro Odetti

Reputation: 452

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

Upvotes: 5

Volodymyr Pavlenko
Volodymyr Pavlenko

Reputation: 671

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'})

Upvotes: 0

Sergey Lyapustin
Sergey Lyapustin

Reputation: 1936

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

Upvotes: 5

Related Questions