In Django's template language, you can use {% url [viewname] [args] %}
to generate a URL to a specific view with parameters. How can you programatically do the same in Python code?
在Django的模板语言中,可以使用{% url [viewname] [args] %}来生成具有参数的特定视图的url。如何在Python代码中以编程方式执行相同的操作?
What I need is to create a list of menu items where each item has name, URL, and an active flag (whether it's the current page or not). This is because it will be a lot cleaner to do this in Python than the template language.
我需要创建一个菜单项列表,其中每个项都有名称、URL和活动标记(无论它是否是当前页面)。这是因为在Python中这样做要比模板语言干净得多。
3 个解决方案
#1
34
If you need to use something similar to the {% url %}
template tag in your code, Django provides the django.core.urlresolvers.reverse()
. The reverse
function has the following signature:
如果您需要在代码中使用类似于{% url %}模板标记的东西,Django提供了Django .core.urlresolvers.reverse()。反向函数具有以下签名:
reverse(viewname, urlconf=None, args=None, kwargs=None)
https://docs.djangoproject.com/en/dev/ref/urlresolvers/
https://docs.djangoproject.com/en/dev/ref/urlresolvers/
#2
8
I'm using two different approaches in my models.py
. The first is the permalink
decorator:
我在我的模型中使用了两种不同的方法。第一个是permalink decorator:
from django.db.models import permalink
def get_absolute_url(self):
"""Construct the absolute URL for this Item."""
return ('project.app.views.view_name', [str(self.id)])
get_absolute_url = permalink(get_absolute_url)
You can also call reverse
directly:
也可以直接调用reverse:
from django.core.urlresolvers import reverse
def get_absolute_url(self):
"""Construct the absolute URL for this Item."""
return reverse('project.app.views.view_name', None, [str(self.id)])
#3
4
Be aware that using reverse()
requires that your urlconf module is 100% error free and can be processed - iow no ViewDoesNotExist
errors or so, or you get the dreaded NoReverseMatch
exception (errors in templates usually fail silently resulting in None
).
请注意,使用reverse()要求您的urlconf模块是100%无错误的,并且可以进行处理——iow没有viewnotexist错误,或者您得到了可怕的NoReverseMatch异常(模板中的错误通常是无声地失败,导致无错误)。
#1
34
If you need to use something similar to the {% url %}
template tag in your code, Django provides the django.core.urlresolvers.reverse()
. The reverse
function has the following signature:
如果您需要在代码中使用类似于{% url %}模板标记的东西,Django提供了Django .core.urlresolvers.reverse()。反向函数具有以下签名:
reverse(viewname, urlconf=None, args=None, kwargs=None)
https://docs.djangoproject.com/en/dev/ref/urlresolvers/
https://docs.djangoproject.com/en/dev/ref/urlresolvers/
#2
8
I'm using two different approaches in my models.py
. The first is the permalink
decorator:
我在我的模型中使用了两种不同的方法。第一个是permalink decorator:
from django.db.models import permalink
def get_absolute_url(self):
"""Construct the absolute URL for this Item."""
return ('project.app.views.view_name', [str(self.id)])
get_absolute_url = permalink(get_absolute_url)
You can also call reverse
directly:
也可以直接调用reverse:
from django.core.urlresolvers import reverse
def get_absolute_url(self):
"""Construct the absolute URL for this Item."""
return reverse('project.app.views.view_name', None, [str(self.id)])
#3
4
Be aware that using reverse()
requires that your urlconf module is 100% error free and can be processed - iow no ViewDoesNotExist
errors or so, or you get the dreaded NoReverseMatch
exception (errors in templates usually fail silently resulting in None
).
请注意,使用reverse()要求您的urlconf模块是100%无错误的,并且可以进行处理——iow没有viewnotexist错误,或者您得到了可怕的NoReverseMatch异常(模板中的错误通常是无声地失败,导致无错误)。