Django - using reverse() on Class-based views

天涯浪子 提交于 2019-12-08 04:44:35

问题


I have the following urls configuration in my Django project:

urlpatterns = patterns('',
    (r'^my-view$', MyViewClass.as_view()),
)

Is there a way to use the reverse() function to get the url of the above view?


回答1:


Yes there is.

Use the name argument of the url function to define a name for the url, then you can use reverse on this name:

from django.conf.urls import patterns, url

urlpatterns = patterns('',
    url(r'^my-view$', MyViewClass.as_view(), name='my_view'),
)

reverse('my_view')


来源:https://stackoverflow.com/questions/25140133/django-using-reverse-on-class-based-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!