Reputation: 2809
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.
Upvotes: 1
Views: 2451
Reputation: 44603
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 %}
Upvotes: 1
Reputation: 13624
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>
Upvotes: 1