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

时间:2021-10-31 10:11:23

I have a form which uses redirect view to create a record when the user pushes a button. This works fine when there is only one argument to be passed. However, I now wish to do this based on two parameters configuration_pk and type_pk and it returns an error:

我有一个表单,它使用重定向视图在用户按下按钮时创建一个记录。当只有一个参数要传递时,这种方法很有效。但是,我现在希望基于两个参数configuration_pk和type_pk来实现这一点,它会返回一个错误:

Reverse for 'new_conf_cert' with arguments '()' and keyword arguments '{u'type_pk': 1, u'configuration_pk': 2}' not found.

逆转为'new_conf_cert'和'{u'type_pk': 1, u'configuration_pk': 2}'。

The template snippet calling this is:

调用这个的模板片段是:

<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>

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

相关在urlconf url(r ' ^添加/ conf_certificate /(? P < configuration_pk >)/(? P < type_pk > \ d +)/ $’,‘Tank.views。new_conf_cert " name = ' new_conf_cert '),

And the views.py section looks like this:

和视图。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 个解决方案

#1


2  

You should fix the regex in urlconf:

您应该在urlconf中修复regex:

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

#1


2  

You should fix the regex in urlconf:

您应该在urlconf中修复regex:

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