Q:

Wrong url regex keeps django busy

I have urls where spaces are replaced with the "-" character.
So I made a url regex like this:    
url(r'^(?P<item_url>(\w+-?)*)/$', 'detail'),

my view:
def detail(request, item_url):
   i = get_object_or_404(Page, url=item_url,published=True)
   return render_to_response('item/detail.html', {'item':i},
       context_instance=RequestContext(request))

Unfortunately this keeps django extremely busy on urls with more than 20 characters. The process hangs for 20sec - 1 minute and then returns the correct result. Is this based on a wrong regex I'm using?

A:

Try the following url pattern:
url(r'^(?P<item_url>[\w-]+)/$', 'detail'),

[\w-]+ will match one or more alphanumeric characters or hyphens.

