我有一个表单,当用户按下按钮时,它使用重定向视图来创建记录。当只有一个参数要传递时,这很好用。但是,我现在希望基于两个参数来执行此操作configuration_pk
,type_pk
并且它返回一个错误:
Reverse for 'new_conf_cert' with arguments '()' and keyword arguments '{u'type_pk': 1, u'configuration_pk': 2}' not found.
调用它的模板片段是:
<div id="menu">
<a><img width="56pt" src="/static/icon-calibrate.svg"></a><br />
<div style="display: none">
{% for type in object.typelist %}
<form action="{% url 'new_conf_cert' configuration_pk=object.id type_pk=type.id %}" method="post" onsubmit="return confirm('Are you sure you want to calibrate all instruments in the {{object}} configuration?')">{% csrf_token %}
<input type="submit" value="Calibrate {{type}}s" />
</form>
{% endfor %}
</div>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
$('#menu > a').click(function(){
$(this).next().next().slideToggle();
return false;
});
</script>
urlconf 中的相关行是
url(r'^add/conf_certificate/(?P<configuration_pk>)/(?P<type_pk>\d+)/$', 'Tank.views.new_conf_cert', name='new_conf_cert'),
views.py 部分如下所示:
class AddConfigurationCertificateView(RedirectView):
http_method_names = ['post']
def get_redirect_url(self, *args, **kwargs):
url = '/Tank/configurations/%s/' % self.kwargs['configuration_pk']
return url
def post(self, request, *args, **kwargs):
conf = Configuration.objects.get(id=self.kwargs['configuration_pk'])
caltype = InstrumentType.objects.get(id=self.kwargs['type_pk'])
for inst in conf.instruments.all():
if inst.kind == caltype:
CalibrationCertificate.objects.create(instrument=inst,
expires=timezone.now()+timedelta(days=inst.kind.duration),
issued=timezone.now(),
issued_by=request.user)
return super(AddConfigurationCertificateView, self).post(request, *args, **kwargs)
new_conf_cert = AddConfigurationCertificateView.as_view()