Reputation: 73235
Suppose I have a Django project that looks like this:
- project - urls.py - items - urls.py - views.py
The root URLconf looks like this:
urlpatterns = patterns('',
url(r'^(?:items/)?$', include('items.urls')),
)
The URLconf for items looks like this:
urlpatterns = patterns('items.views',
url(r'^(?P<item_id>\d+)/$', 'view'),
)
views.py
contains a method:
def view(request, item_id):
item = get_object_or_404(Item, pk=item_id)
return render(request, 'item/view.html', {'item': item})
I'm trying to use django.core.urlresolvers.reverse
in Django's interactive shell but it isn't working:
>>> reverse('items.views.view', kwargs={'item_id': 12}) '/12/'
The problem is that the reversed URL is missing the items/
prefix from the root URLconf. What am I doing wrong?
Upvotes: 0
Views: 498
Reputation: 73235
So it turns out I was doing something dumb:
url(r'^(?:items/)?$', include('items.urls')),
Because the 'items' is optional, the reverse
method just ignores it and outputs '/'.
Upvotes: 1