Django基于函数名的反向类视图不起作用 [英] Django reverse class based views by function name do not work

查看:158
本文介绍了Django基于函数名的反向类视图不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据django文档,viewname是函数名称或url模式名称。但是,像这样反转一个url(MyView.as_view())变成一个NoReverseMatch异常。有没有办法通过函数名来反转基于类的视图?

According to the django docs, viewname is either the function name or the url pattern name. But reversing an url like this 'reverse(MyView.as_view())' turns into a NoReverseMatch exception. Is there any way to reverse class based view by function name?

推荐答案

你可以使用指定的网址格式,或者您可以执行以下操作(在视图中) .py

You can either used named url patterns or you can do something like the following (in your views.py)

my_function = MyView.as_view()

现在反向工作: reverse('myviews.my_function')

这篇关于Django基于函数名的反向类视图不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆