[英]How do I make a form field read only or otherwise non editable in a django generic FormView?
我有一个表单,该表单会使用当前登录的用户预填充页面加载时的用户字段。 我希望将此字段显示为灰色,以便用户无法将其更改为未登录的用户。我希望该用户能够更改其余字段。 我试图在模型窗体和视图上使用init函数无济于事。 任何指针将不胜感激。
forms.py:
class PtoRequestForm(forms.ModelForm):
class Meta:
# Model to be used in the form.
model = PtoHistory
# Define which model fields to include in the form.
fields = [
'user',
'start',
'end',
'leave_type',
]
# Attach input widgets to fields for a friendlier user interface.
widgets = {
'start': DateTimeWidget(attrs={'id':'start'}, usel10n = True, bootstrap_version=3),
'end': DateTimeWidget(attrs={'id':'end'}, usel10n = True, bootstrap_version=3),
}
views.py:
class IndexView(FormView):
template_name = 'accounts/index.html'
form_class = PtoRequestForm
success_url = '/accounts'
# Pre-populates the form with the specified values on page load.
def get_initial(self):
return {'user': self.request.user}
# Function runs when form is submitted. Do whatever needed to the form before it's saved to the db.
def form_valid(self, form):
form.save()
return super(IndexView, self).form_valid(form)
# Function runs on page load. Gather any information needed for the template.
def get_context_data(self, **kwargs):
# Initialize context
context = super(IndexView, self).get_context_data(**kwargs)
# Grab all ptoHistory records from the database, selecting only the values desired.
pto_history = PtoHistory.objects.values('start', 'end', 'pk', 'user_id')
# Convert the django queryset to json so that fullcalendar can work with it on the frontend.
json_pto_history = json.dumps(list(pto_history), cls=DjangoJSONEncoder)
# Save the converted json to the context to be returned to the front end.
context['ptoHistory'] = json_pto_history
return context
这是我的表单的屏幕截图。 用户不应该能够更改用户字段,但是其余字段可以更改。
您可以如下添加它:
class PtoRequestForm(forms.ModelForm):
class Meta:
# Model to be used in the form.
model = PtoHistory
# Define which model fields to include in the form.
fields = [
'user',
'start',
'end',
'leave_type',
]
# Attach input widgets to fields for a friendlier user interface.
widgets = {
'start': DateTimeWidget(attrs={'id':'start'}, usel10n = True, bootstrap_version=3),
'end': DateTimeWidget(attrs={'id':'end'}, usel10n = True, bootstrap_version=3),}
def __init__(self, *args, **kwargs):
super(PtoRequestForm, self).__init__(*args, **kwargs)
self.fields['user'].widget.attrs['readonly'] = True
在我的模型表单中,我没有像这样包含“用户”字段:
forms.py:
class PtoRequestForm(forms.ModelForm):
class Meta:
# Model to be used in the form.
model = PtoHistory
# Define which model fields to include in the form.
fields = [
'start',
'end',
'leave_type',
]
# Attach input widgets to fields for a friendlier user interface.
widgets = {
'start': DateTimeWidget(attrs={'id':'start'}, usel10n = True, bootstrap_version=3),
'end': DateTimeWidget(attrs={'id':'end'}, usel10n = True, bootstrap_version=3),
}
然后在我的form_valid函数中,我只是将当前登录的用户附加到我的数据库中,如下所示:
views.py:
def form_valid(self, form):
new_form = form.save(commit=False)
new_form.user = self.request.user
new_form.save()
return super(IndexView, self).form_valid(form)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.