如何将多个url参数传递给RedirectView?

时间:2013-09-02 14:11:20

标签: python django

我有一个表单,当用户按下按钮时,该表单使用重定向视图来创建记录。当只有一个参数要传递时,这很好。但是,我现在希望基于两个参数configuration_pktype_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()

1 个答案:

答案 0 :(得分:2)

您应该在urlconf中修复正则表达式:

url(r'^add/conf_certificate/(?P<configuration_pk>\d+)/(?P<type_pk>\d+)/$', 'Tank.views.new_conf_cert', name='new_conf_cert')