How to use reverse() from django.core.urlresolvers.reverse

牧云@^-^@ 提交于 2019-12-25 04:19:32

问题


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.

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).


回答1:


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')
)

using python manage.py shell in your project directory you do the following:

>>> from django.core.urlresolvers import reverse
>>> reverse('recent')
'/recent'

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'

check the doc on reverse for your particular version of Django.



来源:https://stackoverflow.com/questions/22143265/how-to-use-reverse-from-django-core-urlresolvers-reverse

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