开发者

passing url matching parameter to reverse() in urls.py

开发者 https://www.devze.com 2023-03-25 14:26 出处:网络
in my urls.py I need to invoke a generic CreateView that requires a success_url parameter. The \"success\" URL contains an identifier that I need to pass to the reverse() URL search function. I get th

in my urls.py I need to invoke a generic CreateView that requires a success_url parameter. The "success" URL contains an identifier that I need to pass to the reverse() URL search function. I get this parameter from the URL of the CreateView. please see the code below. I need to grab the value of the <pk> parameter in the "create" url, and pass it on to the "success" url. how is this done?

thanks

konstantin

PS: using django trunk

...
url(r'^path/(?P<pk>\d+)/apply/$',
    generic.CreateView.as_view(form_class=MyForm,
                               success_url=reverse_lazy('success', args=[???<pk>???]),
                               template_name='create.html'), name='create'),
url(r'path/(?P<pk>\d+)/apply/success/$',
    generic.TemplateView.as_view(template_name='success.html'), n开发者_开发问答ame='success'),
...


This is explained in the documentation:

success_url may contain dictionary string formatting, which will be interpolated against the object's field attributes. For example, you could usesuccess_url="/polls/%(slug)s/" to redirect to a URL composed out of the slug field on a model.

0

精彩评论

暂无评论...
验证码 换一张
取 消