中的Django引用url视图.py

2024-07-08 12:39:59 发布

您现在位置:Python中文网/ 问答频道 /正文

在Django,我已经准备好了网址.py如下所示:

url(r'^example/$',   ExampleView.as_view(), name='example'),
url(r'^example2/$',   AnotherView.as_view(), name='example2'),

其中“example2”应该是:'http://localenv.com/example2'。在

在我的视图.py,我想返回对“example2”链接的引用。让我解释一下:

^{pr2}$

我希望print语句返回“http://localenv.com/example2

有什么帮助吗?在


Tags: djangonamepycomview视图httpurl
2条回答

您将要使用^{}。在

from django.core.urlresolvers import reverse

class ExampleView(TemplateView):
    some_var = reverse('example2')
    print some_var

编辑:

如果需要绝对uri,可以使用^{}构建它

^{pr2}$

你可以看看urlresolvers。但是它只提供URI,您必须手动添加“http://localenv.com…”。在

相关问题 更多 >

    热门问题