Reputation: 439
I have a form for an account and I wish to make it ReadOnly and Updatable.
How should I handle the switch from readonly True to False?
I know about this:
self.fields['fieldname'].widget.attrs.update({'class': 'form-control', 'readonly': True})
I hope that there is a way to remove readonly on view so that I won't need to create a separate form.
Or you can also suggest other solution for my concern. Or should I create a separate Form for readonly?
Upvotes: 1
Views: 306
Reputation: 2084
In your html file, say you this form
<form method="post" action="{% url 'some_view' %}">
<input type="text" value="something" name="field1" readonly>
<input type="text" value="something1" name="field2" >
<input type="text" value="something2" name="field3">
<input type="submit" >
</form>
Now in your views.py, you can read all the three variables using the name of the tag
views.py
def func(request):
if request.method == 'POST':
field1 = request.POST['field1']
field2 = request.POST['field2']
field3 = request.POST['field3']
#Do Db update or any other
return HttpResponse('success')
You can do in the above way
Upvotes: 2