0

我有一个UserFormforms.py,它在RegisterEdit Profile特性之间很常见,它的视图也很常见,因为90%的功能在两个视图中都是相似的。

我只想disable在页面EMailField中使用,但应该在UserForm页面中可编辑。这是我的代码:Edit ProfileRegister

表格.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>
4

1 回答 1

1

在您的 update_views 中,您可以设置:
form.fields['email'].disabled = True

于 2020-08-26T12:00:19.410 回答