我有一个UserForm
forms.py,它在Register
和Edit Profile
特性之间很常见,它的视图也很常见,因为90%的功能在两个视图中都是相似的。
我只想disable
在页面EMailField
中使用,但应该在UserForm
页面中可编辑。这是我的代码:Edit Profile
Register
表格.py
class UserForm(forms.ModelForm):
password = None
email = forms.EmailField(required=False, disabled=True) #-- This will disable all the times
salutation = forms.ChoiceField(choices=Choices.SALUTATIONS)
blood_group = forms.ChoiceField(choices=Choices.BLOOD_GROUPS)
class Meta:
model = User
fields = ('salutation', 'blood_group', 'email', 'first_name', 'middle_name', 'last_name',
'gender', 'birth_date', 'profile')
register
在时间和edit
视图中都呈现相同的表单。这是我的 urls.py。
path('register/', StudentView.as_view(), name='addview'),
path('<int:pk>/edit/', StudentView.as_view(), name='editview'),
在 views.py 中(有关详细的 views.py,请检查接受的答案)
userform = UserForm(request.POST or None, instance=self.object)
录取.html
<form class="form" name="admissionForm" id="admissionForm" method="post"
enctype="multipart/form-data" action="{% url 'editview' form.instance.id %}">
{% csrf_token %}
<div class="pages">
<h4 class="mt-2 mb-3">Personal Information</h4>
{% include "student_errors.html" %}
{{userform|crispy}}
....
<div class="btn_container">
<button type="submit" class="btn btn-info float-right btn-next">Submit</button>
</div>
</div>