我的 ModelForm 没有自动填充
My ModelForm isn't getting auto-populated
在更新模板中,我的表单没有被预填充,但是更新功能运行良好。当我尝试解析特定 ID
的实例时,表单保持为空
我的view.py:
def update_component(request, pk):
component = Component.objects.all()
component_id = Component.objects.get(id=pk)
form = ComponentModelForm(instance=component_id)
if request.method == 'POST':
form = ComponentModelForm(request.POST, instance=component_id)
if form.is_valid():
form.save()
return HttpResponseRedirect(request.path_info)
context = {
'components': component,
'form': ComponentModelForm(),
'component_id':component_id,
}
return render(request, 'update_component.html', context)
模板中的表单:
<div>
{% load widget_tweaks %}
<form class="component-form-flex" method='POST' action=''>
{% csrf_token %}
<div style="width:50%;" >
<br>
<span class="component-label-text">Name</span>
{% render_field form.name class="component-form" %}
<span class="component-label-text">Manufacturer</span>
{% render_field form.manufacturer class="component-form" %}
<span class="component-label-text">Model</span>
{% render_field form.model class="component-form" %}
<span class="component-label-text">Serial Number</span>
{% render_field form.serial_number class="component-form" %}
<span class="component-label-text">Price</span>
{% render_field form.price class="component-form" %}
<span class="component-label-text">Note</span>
{% render_field form.note class="component-form" %}
{% render_field form.parent class="component-form" %}
<input type="submit" class="form-submit-button" value='Update Component' />
</div>
<div>
<img class="maintenance-nav-list-img" src="{{ component_id.image.url }}" />
{% render_field form.image %}
</div>
</form>
</div>
urls.py :
from django.urls import path
from . import views
appname = 'maintenance'
urlpatterns = [
path('', views.index , name='maintenance'),
path('<int:pk>/update/', views.update_component , name='update_component'),
]
这只是一个错字,而不是
context = {
'components': component,
'form': ComponentModelForm(),
'component_id':component_id,
}
应该是
context = {
'components': component,
'form': form, # <--- HERE !!!!
'component_id':component_id,
}
在更新模板中,我的表单没有被预填充,但是更新功能运行良好。当我尝试解析特定 ID
的实例时,表单保持为空我的view.py:
def update_component(request, pk):
component = Component.objects.all()
component_id = Component.objects.get(id=pk)
form = ComponentModelForm(instance=component_id)
if request.method == 'POST':
form = ComponentModelForm(request.POST, instance=component_id)
if form.is_valid():
form.save()
return HttpResponseRedirect(request.path_info)
context = {
'components': component,
'form': ComponentModelForm(),
'component_id':component_id,
}
return render(request, 'update_component.html', context)
模板中的表单:
<div>
{% load widget_tweaks %}
<form class="component-form-flex" method='POST' action=''>
{% csrf_token %}
<div style="width:50%;" >
<br>
<span class="component-label-text">Name</span>
{% render_field form.name class="component-form" %}
<span class="component-label-text">Manufacturer</span>
{% render_field form.manufacturer class="component-form" %}
<span class="component-label-text">Model</span>
{% render_field form.model class="component-form" %}
<span class="component-label-text">Serial Number</span>
{% render_field form.serial_number class="component-form" %}
<span class="component-label-text">Price</span>
{% render_field form.price class="component-form" %}
<span class="component-label-text">Note</span>
{% render_field form.note class="component-form" %}
{% render_field form.parent class="component-form" %}
<input type="submit" class="form-submit-button" value='Update Component' />
</div>
<div>
<img class="maintenance-nav-list-img" src="{{ component_id.image.url }}" />
{% render_field form.image %}
</div>
</form>
</div>
urls.py :
from django.urls import path
from . import views
appname = 'maintenance'
urlpatterns = [
path('', views.index , name='maintenance'),
path('<int:pk>/update/', views.update_component , name='update_component'),
]
这只是一个错字,而不是
context = {
'components': component,
'form': ComponentModelForm(),
'component_id':component_id,
}
应该是
context = {
'components': component,
'form': form, # <--- HERE !!!!
'component_id':component_id,
}