2012-08-14 16 views
6

मैं है प्रपत्र क्षेत्र {{form.item}} कोका उपयोग कर

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

कैसे मैं का उपयोग कर प्रपत्र फ़ील्ड का नाम विशेषता बदल सकते हैं प्रदान करेगा जो Django टेम्पलेट में प्रपत्र फ़ील्ड का नाम विशेषता बदलें कस्टम टेम्पलेट टैग?

मैं टेम्पलेट टैग जहां

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

लेकिन मैं सफलता नहीं मिल रहा है करने के लिए प्रपत्र भेजकर की कोशिश की।

मुझे टेम्पलेट में नाम विशेषता बदलने की जरूरत है।

अद्यतन

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, 
     } 

टेम्पलेट

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

अब कस्टम टेम्पलेट टैग में मैं इनलाइन formset के प्रत्येक क्षेत्र

+0

संभव डुप्लिकेट [ओवरराइड Django प्रपत्र क्षेत्र का नाम attr] (http://stackoverflow.com/questions/8801910/override-django-form-fields-name-attr) – mgibsonbr

+1

क्यों क्या आप नाम बदलना चाहते हैं? आपको क्या लगता है कि आपको इसकी आवश्यकता है? –

+0

एर, क्या? उनमें से कोई भी गतिशील नामों का उपयोग करने का एक कारण नहीं है। अपने उपयोग के मामले की व्याख्या करें, और यह मानक दृश्य/स्वरूप संरचना से क्यों कवर नहीं है। –

उत्तर

0

आप किसी भी विजेट वर्ग आपको क्या चाहिए उपवर्ग और अपने खुद के "प्रस्तुत करना विधि" बना सकते हैं के लिए नया नाम सौंपने होंगे। उदाहरण PATH_TO_YOUR_DJANGO/Django/रूपों में कर रहे हैं/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

प्रश्न अपडेट किया है, मैं नहीं देख रहा हूं कि आप कहां हैं फील्ड नाम विशेषता – Asif

2
form.fields['new_name'] = form.fields['item'] 
del form.fields['item'] 
+0

@Sergy के लिए नया नाम निर्दिष्ट कर रहे हैं: मैंने पहले से ही यह कोशिश की है, लेकिन मेरे मामले में यह काम नहीं करेगा क्योंकि मैं फॉर्मेट से काम कर रहा हूं – Asif

4

मैं इस में कुछ अलग अलग तरीकों से परीक्षण किया है, और यह फार्म फील्ड्स के कई प्रकार के साथ काम करता है।

प्रत्येक फ़ील्ड पर set_field_html_name(...) का उपयोग करें जिसे आप नाम सेट करना चाहते हैं।

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 
1
class MyForm(forms.ModelForm): 
    def __init__(self, *args, **kwargs): 
     super(MyForm, self).__init__(*args, **kwargs) 
     self.fields['field_name'].label = "New Field name" 
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'})) 
की
संबंधित मुद्दे