问题描述
如何在命令行中使用 django.core.urlresolvers.reverse
中的 reverse()
我想调试我的Django应用程序出了什么问题。我不确定是否在视图
, urls
或 html
模板页。
How do you use the reverse()
from django.core.urlresolvers.reverse
at the command line? I want to debug what is going wrong in my Django application. I am not sure if it is happening at the views
, the urls
or the html
template page.
我在项目目录中打开了命令行,但不能识别我的命令(我正在借用从Django-Project页面)。
I have the command line open in the directory of the project, but it doesn't recognize my commands (which I am borrowing from the Django-Project page).
推荐答案
如果您的 urls.py
文件包含如下内容:
If your urls.py
file consists on something like this:
urlpatterns = patterns('',
url(r'^$', 'views.recent', name='recent'),
url(r'^recent/(?P<page>\d+)$', 'views.recent', name='recent')
)
请执行以下操作:
>>> from django.core.urlresolvers import reverse
>>> reverse('recent')
'/recent'
可以传递特定参数列表为 args
或字典为 kwargs
you can pass specific parameters passing a list as args
or a dictionary as kwargs
>>> reverse('recent', args=[1])
'/recent/1'
>>> reverse('recent', kwargs={'page': 2})
'/recent/2'
查看您的特定版本的Django的 。
check the doc on reverse for your particular version of Django.
这篇关于如何使用reverse()从django.core.urlresolvers.reverse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!