python - Django 使用表记录预填充 ModelForm
问题描述
我正在尝试创建一个允许用户滚动表格并单击每行中存在的按钮以编辑模式中的数据的编辑。
我遇到的问题是当我尝试使用现有值预填充表单时。如何转换下面的 EditCommunicationsForm 以显示您选择编辑的行的当前值?
表格.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 中获取这些值,并可以在所需的位置进行渲染。同时使用一种模型形式进行创建和编辑。
推荐阅读
- sql - 基于行划分,没有多个查询
- java - Vaadin Spring Boot 找不到 npm 包
- python-3.x - Python中是否有用于多元数据分析的功能
- c# - 有没有办法在 DuplexChannelFactory<> 中有多个回调?
- flutter - Flutter 应用程序,Theme.of(context) 样式不适用于文本
- c# - 如何使自定义 ModelBinder 自动应用于某种类型的所有属性
- reactjs - 无法在反应项目中使用recordRTC
- twilio - 如何在 Twilio 视频室中检测按键音 (DTMF)
- oracle - Oracle 手册失败——使用“Accessible By”子句的包演示抛出异常
- c++ - 项目中类的 C++ 未解决的外部问题