tags:

views:

248

answers:

3

In my Django project, I used to have a single URLConf, urls.py at the root of the project. This URLConf included some named URLs using Django's url() function. In several templates, I reference these URLs with the url tag, à la {% url named_url %}. This worked fine.

The root urls.py became a bit unwieldy, so I split it off into a URLConf for each app, in app/urls.py. Some URLs still have names. Unfortunately, I get a TemplateSyntaxException when using the url tag in templates now. Specifically, the error message is:

Caught an exception while rendering: Reverse for 'myproj.myapp.new_test' with arguments '()' and keyword arguments '{}' not found.

Is there a way to reference the named URLs in the app-specific URLConfs using the url tag in Django?

A: 

I can recommend you two things:

  • Use name in url patterns
  • Do not do references to the project name inside a app(like you did with "myproj.myapp.new_test". If this was the "right way to do", you should only reference as "myapp.new_test"
Tiago
I _am_ using names in URL patterns, as I noted in the question. And I'm _not_ "doing references to the project name", but Django gives the full view path (including the project name) in its error message.
mipadi
Sorry. I read that, but I thought otherwise because of the error.
Tiago
A: 

You definitely can reference urls in included urlconfs via the url tag - that's in fact what you're supposed to do. However, I've always found the url tag and the reverse() function to be very flaky and error-prone, so these errors do sometimes occur.

My suggestion would be to give all your urls a name, no matter which urlconf they are in. Then you just need to refer to the actual name - you don't need to qualify it with the name of the app or urlconf or anything. See if that works.

Daniel Roseman
A: 

Are you referencing each app's urls.py?

from django.conf.urls.defaults import *

urlpatterns = patterns('',
    (r'^app1/',        include('app1.urls')),
    (r'^app2/', include('app2.urls')),
)

from Django Docs

jobscry
Yes. The URLs themselves work fine (as in, I can navigate to them without getting a 404). It's just the use of {% url %} that doesn't seem to work.
mipadi
can you post some of your code?
jobscry