【发布时间】:2019-06-14 17:15:45
【问题描述】:
我想在从下拉列表或自动完成列表中选择产品后单击表单按钮 (UPDATE) 从页面更新/编辑产品。
List.html 页面工作正常,而单击更新按钮 update.html 页面无法解析 POST 数据。
手动我可以使用 pk 后缀(/update/1/)访问update.html,它也可以正常工作。
如何将 pk 的值单独传递给 url?
views.py
class ProductUpdateView(UpdateView):
template_name = 'update.html'
model = Product
fields = ['name', 'description', 'image', 'file',]
success_url = '/list/'
class ProductsView(ListView,):
template_name = 'list.html'
model = Product
urls.py
urlpatterns = [
url(r'^list/$', ProductsView.as_view(), name='list'),
url(r'^update/(?P<pk>[0-9]+)/$', ProductUpdateView.as_view(), name='update'),
]
list.html
<body>
<form method='POST' action='/update/'> {% csrf_token %}
<select name='pk'>
{% for obj in object_list %}
<option value='{{ obj.id }}'>{{ obj.name }}</option>
{% endfor %}
</select>
<input type="submit" value='UPDATE'>
</form>
</body>
【问题讨论】:
-
您可以在选择框的更改事件上发出ajax请求。
-
您的问题中没有包含
update.html页面。告诉我们你是如何更新你的 html 的。