这显然是我在这里挣扎的一个基本概念,但从来没有像现在这样。我可以从管理界面中选择项目并进入中间模板页面来填充数据,但是当我点击提交或取消时,POST数据似乎被丢弃为" set_location"似乎永远不会调用函数来处理返回的POST数据。我不知道如何将表单中的数据发送回" set_location" admin_action函数。
form.py中的:
class LocationForm(forms.Form):
country = forms.CharField(max_length=50)
city_region = forms.CharField(max_length=100)
admin.py中的:(我已为我的某个管理模型编写了管理操作)
def set_location(self, request, queryset):
if request.POST.get('post'):
# process the queryset here
if 'cancel' in request.POST:
self.message_user(request, 'Set location info operation cancelled.')
return
if form.is_valid():
request_country = form.cleaned_data['country']
request_city_region = form.cleaned_data['city_region']
rows_updated = queryset.update(country=request_country, city_region=request_city_region)
self.message_user(request, "Location information successfully applied for %s serial number(s)." % rows_updated)
return
else:
context = {
'title': "Set geographic data.",
'queryset': queryset,
'form': LocationForm,
'path': request.get_full_path()
}
return TemplateResponse(request, 'set_location.html',
context, current_app=self.admin_site.name)
self.message_user(request, "Control should never get here... whoops")
set_location.short_description = 'Apply location info to selected Serials'
来自set_location.html的:
<html>
<head>
<title>{{ title }}</title>
</head>
<form action="" method="post">{% csrf_token %}
<p>Please set the location information for the serial numbers below:</p>
{{ form.as_p }}
<input type="hidden" name="action" value="set_location" />
<input type="hidden" name="post" value="yes" />
<input type="submit" name="cancel" value="Cancel" />
<input type="submit" value="Submit" />
<p>Note: The location information specified above will apply to the following serial numbers:</p>
<ul>{{ queryset|unordered_list }}</ul>
</form>
</html>
大部分内容都是复制粘贴在一起我只是不了解模板,视图和表单是如何进行交互的。我觉得没有必要编写一个视图来处理这个信息,但我似乎无法弄清楚如何在我需要的地方获取提交的POST数据。有什么想法吗?