如何预填充 Django ModelForm 字段
How to pre-populate Django ModelForm fields
我有以下模型:
class IssueProcessForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(IssueProcessForm, self).__init__(*args, **kwargs)
self.fields['number'].disabled = True
self.fields['title'].disabled = True
self.fields['body'].disabled = True
self.fields['body'].widget = forms.Textarea(
attrs={
'cols': 50
}
)
class Meta:
model = Issue
fields = (
'number', 'title', 'body', 'price'
)
我想用视图中的数据预填充字段 number
、title
和 body
,呈现表单,并确保值与字段对应显示同时也禁用这些字段,以便用户不会更改值。我希望 price
字段是用户触摸的唯一内容,当单击保存按钮时,我希望所有内容都保存到数据库中。我尝试了以下方法:
def issue_process(request, repo_name, issue_number):
get_issue_number = request.session.get('issue_number_{}'.format(issue_number))
get_issue_title = request.session.get('issue_number_{}_title'.format(issue_number))
get_issue_body = request.session.get('issue_number_{}_body'.format(issue_number))
if request.method == 'POST':
form = IssueProcessForm(request.POST)
if form.is_valid():
issue = form.save(commit=False)
issue.number = get_issue_number
issue.title = get_issue_title
issue.body = get_issue_body
issue.save()
else:
form = IssueProcessForm(initial={
'number': get_issue_number,
'title': get_issue_title,
'body': get_issue_body
})
return render(request, 'core/issue_process.html', {'form': form})
...但是当我尝试提交时,三个字段中的每一个都一直显示 this field is required
。请问我该怎么办?请注意 get_issue_number
、get_issue_title
、get_issue_body
是我想预填充字段 number
、title
和 [=14= 的值]分别与
禁用表单域后,浏览器不会在您提交表单时提交该域的值。因此,您需要为 POST 请求和 GET 请求提供初始数据:
if request.method == 'POST':
form = IssueProcessForm(
request.POST,
initial={
'number': get_issue_number,
'title': get_issue_title,
'body': get_issue_body
},
)
...
我有以下模型:
class IssueProcessForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(IssueProcessForm, self).__init__(*args, **kwargs)
self.fields['number'].disabled = True
self.fields['title'].disabled = True
self.fields['body'].disabled = True
self.fields['body'].widget = forms.Textarea(
attrs={
'cols': 50
}
)
class Meta:
model = Issue
fields = (
'number', 'title', 'body', 'price'
)
我想用视图中的数据预填充字段 number
、title
和 body
,呈现表单,并确保值与字段对应显示同时也禁用这些字段,以便用户不会更改值。我希望 price
字段是用户触摸的唯一内容,当单击保存按钮时,我希望所有内容都保存到数据库中。我尝试了以下方法:
def issue_process(request, repo_name, issue_number):
get_issue_number = request.session.get('issue_number_{}'.format(issue_number))
get_issue_title = request.session.get('issue_number_{}_title'.format(issue_number))
get_issue_body = request.session.get('issue_number_{}_body'.format(issue_number))
if request.method == 'POST':
form = IssueProcessForm(request.POST)
if form.is_valid():
issue = form.save(commit=False)
issue.number = get_issue_number
issue.title = get_issue_title
issue.body = get_issue_body
issue.save()
else:
form = IssueProcessForm(initial={
'number': get_issue_number,
'title': get_issue_title,
'body': get_issue_body
})
return render(request, 'core/issue_process.html', {'form': form})
...但是当我尝试提交时,三个字段中的每一个都一直显示 this field is required
。请问我该怎么办?请注意 get_issue_number
、get_issue_title
、get_issue_body
是我想预填充字段 number
、title
和 [=14= 的值]分别与
禁用表单域后,浏览器不会在您提交表单时提交该域的值。因此,您需要为 POST 请求和 GET 请求提供初始数据:
if request.method == 'POST':
form = IssueProcessForm(
request.POST,
initial={
'number': get_issue_number,
'title': get_issue_title,
'body': get_issue_body
},
)
...