gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:55:49 25 4
gpt4 key购买 nike

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

最佳答案

您应该在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com