[英]Django Prepopulate ModelForm with Table Record
我正在尝试创建一个允许用户滚动表格并单击每行中存在的按钮以编辑模式中的数据的编辑。
我遇到的问题是当我尝试使用现有值预填充表单时。 如何转换下面的 EditCommunicationsForm 以显示您选择编辑的行的当前值?
forms.py
class CommunicationsForm(forms.ModelForm):
class Meta:
model = Communications
fields = "__all__"
widgets = {
'project':forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter your Project Name'}),
'title':forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter a short Title'}),
'intent':forms.Textarea(attrs={'class': 'form-control','height':'50px','placeholder':'Describe the intent and desired outcome of the communication'}),
'date':forms.TextInput(attrs={'class': 'form-control','placeholder':'Select a Date'}),
'channel':forms.Select(attrs={'class': 'form-control'}),
'content_type':forms.Select(attrs={'class': 'form-control','placeholder':'Select a Content Type'}),
'audience':forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter the Audience(s)'}),
'status':forms.Select(attrs={'class': 'form-control','placeholder':'Select the Status'}),
}
class EditCommunicationsForm(forms.ModelForm):
class Meta:
model = Communications
fields = "__all__"
widgets = {
'project':forms.TextInput(attrs={'class': 'form-control','initial':'Project 123'}),
'title':forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter a short Title'}),
'intent':forms.Textarea(attrs={'class': 'form-control','height':'50px','placeholder':'Describe the intent and desired outcome of the communication'}),
'date':forms.TextInput(attrs={'class': 'form-control','placeholder':'Select a Date'}),
'channel':forms.Select(attrs={'class': 'form-control'}),
'content_type':forms.Select(attrs={'class': 'form-control','placeholder':'Select a Content Type'}),
'audience':forms.TextInput(attrs={'class': 'form-control','placeholder':'Enter the Audience(s)'}),
'status':forms.Select(attrs={'class': 'form-control','placeholder':'Select the Status'}),
}
视图.py
def communications(request):
comms_list = Communications.objects.order_by('id')
if request.method == "POST":
new_form = CommunicationsForm(request.POST, request.FILES)
edit_form = EditCommunicationsForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('http://127.0.0.1:7000/polls/communications/',{"new_form":new_form,"edit_form":edit_form,'comms_list':comms_list})
else:
comms = Communications.objects.get(id=**ID**)
new_form = CommunicationsForm()
edit_form = EditCommunicationsForm(initial={"project":comms.project })
query = request.GET.get('search')
if query:
postresult = Communications.objects.filter(id__contains=query)
comms_list = postresult
else:
comms_list = Communications.objects.order_by('id')
return render(request,'polls/communications.html',{"new_form":new_form,"edit_form":edit_form,'comms_list':comms_list})
网址.py
path('communications/', views.communications, name='communications'),
通讯.html
<!--Edit Modal-->
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Create User</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form method="POST">
{% csrf_token %}
{{edit_form}}
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" value="update" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
</div>
您需要传递表单的初始值。
communications = Communications.objects.get(id=**ID**)
edit_form = EditCommunicationsForm(initial={"project":communications.project, })
您将在 html 中获得这些值,并可以在所需位置进行渲染。 同时使用一个 model 表格用于创建和编辑目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.