1

I have typical profile/account settings form. So as expected you would be having a "name" field and a "domain" field(which will be used as like a website url: maddy.mywebsite.com). This domain field needs to be editable only once. Once set to one thing, will not be made editable later(since this is like a site url).

At the model level, I am comparing created_on and modified_on of that object(userprofile) and validating "domain" value saving. But at the form level, how do I customize the rendering of that field alone based on the condition I have taken ?

Note 1: I don't want to move the "domain" field to the signup page.

Note 2: I am trying to use normal forms(django.forms) and not a ModelForm.

Nanda Kishore
  • 2,789
  • 5
  • 38
  • 61

2 Answers2

1

You may try using two htmls, One for create profile and one for editing profile. Render the complete form in create profile and for editing profile if you use same django you may disable the #id_name and #id_domain filed by using either css or javascript. An implementation using js:

    <script type="text/javascript"> 
        var domain = document.getElementById("id_domain");
        var name = document.getElementById("id_name");
        domain.value = "{{ domain }}";
        name.value = "{{ name }}";
        domain.readOnly = true;        
    </script>
crodjer
  • 13,384
  • 9
  • 38
  • 52
  • I don't think it's a good idea to trust the browser/user to not modify the value. Django will still save the (hopefully unchanged) domain in this case and there's nothing stopping the user altering it with FireBug or similar. – Tom Nov 12 '10 at 04:03
  • You may try using two different django forms instead. The one for editing profile with name and domain fields disabled and render the name and domain from database separately. – crodjer Nov 12 '10 at 04:07
  • @dcrodjer:I want to avoid two forms as having them would be tough to maintain. – Nanda Kishore Nov 13 '10 at 05:04
  • @Tom: I agree with Firebug hack. It would be best to display the domain field as a label(or free text) instead of a readonly or a disabled textfield. – Nanda Kishore Nov 13 '10 at 05:05
  • @maddy you may render only the fields you want to using the template....dont use {{ form }} for the edit form and render only the fields you want to...http://docs.djangoproject.com/en/dev/topics/forms/#customizing-the-form-template – crodjer Nov 13 '10 at 07:54
  • @dcrodjer thats what I arrived at now. But I am trying to find an elegant solution. {{form.domain}} should render a text field or simple label (in different situations) seamlessly. Thats the goal. – Nanda Kishore Nov 13 '10 at 09:01
  • i think for that you have to define a custom form for editing where the name and domain fields are rendered disabled – crodjer Nov 13 '10 at 11:42
1

This looks tricky, you can read some of the following for some ideas:

In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?

http://lazypython.blogspot.com/2008/12/building-read-only-field-in-django.html

None of those are particularly simple though, so I'd suggest (as did @dcrodjer):

Creating two forms, one for creating, one for editing. On the editing form, remove the Domain field so it's not required/won't be saved:

# forms.py

class AddForm(ModelForm):
    class Meta:
        model = UserProfile
        fields = ('name','domain',)  

class EditForm(ModelForm):
    class Meta:
        model = UserProfile
        fields = ('name',)  

In your view, creating the appropriate form, and in your template, rendering different HTML depending on which form you've been given:

{% if form.domain %}
    {{form.domain.label_tag}}: {{form.domain}} {{form.domain.errors}}
{% else %}
    Domain: <input type="text" disabled="disabled" value="{{userprofile.domain}}"/>
{% endif %}
Community
  • 1
  • 1
Tom
  • 42,844
  • 35
  • 95
  • 101
  • I want to avoid two forms(and particularly ModelForms) – Nanda Kishore Nov 13 '10 at 05:05
  • Using a ModelForm is irrelevant in this case. If you want to avoid using two forms, I say just do the {% if %} in the template then. Django doesn't have a readonly attribute you can set after a form has been created by a view, so there's no "right" way. – Tom Nov 14 '10 at 06:39