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

标签 python django

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

最佳答案

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

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

关于python - 如何将多个 url 参数传递给 RedirectView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18575207/

相关文章:

python - 为什么这个测试函数不输出图像? pokeGAN 教程

python - Pandas - 识别项目的最后一个条目

python - 查找模式是否在两个字符串中的任何一个中的最短方法

css - django admin 没有显示一些 css 和表单功能

python - 添加自定义 Facebook 选项卡应用程序时,“X-Frame-Options”设置为 'SAMEORIGIN'

python - 异常 : Jupyter command `jupyter-notebook` not found, 窗口

python - 使用 MRJob 更改 Mapreduce 中间输出位置

python - 如何根据 sku 从 woocommerce api 获取产品?

带有 PostgreSQL 10 的 Django?

python - MagicMock 对象可以迭代吗?