Opened 7 years ago

Closed 7 years ago

#29365 closed Bug (invalid)

the reverse function does not work in the current application, which was included in the URL using namespace

Reported by: blins Owned by: nobody
Component: Core (URLs) Version: 2.0
Severity: Normal Keywords: reverse url current_app
Cc: blins@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

in app/urls.py

urlpatterns = [
    url(r'^$', Index.as_view(), name = 'index'),
    url(r'^all/$', All.as_view(), name = 'all'),
]

in project urls.py

...
    path('app/', include('app.urls', namespace='app')),
...

and in index template app/index.html

{%url all %}

function produces an error "NoReverseMatch at /app/. Reverse for 'all' not found. 'all' is not a valid view function or pattern name."

Attachments (2)

base.py.patch (1.3 KB ) - added by blins 7 years ago.
patch
project.zip (13.2 KB ) - added by blins 7 years ago.
Sample project

Download all attachments as: .zip

Change History (6)

by blins, 7 years ago

Attachment: base.py.patch added

patch

comment:1 by Tim Graham, 7 years ago

When I setup ULRconfs as you describe, I see "ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead." Maybe the steps to reproduce aren't complete... can you provide a sample project?

comment:2 by Tim Graham, 7 years ago

Resolution: needsinfo
Status: newclosed

by blins, 7 years ago

Attachment: project.zip added

Sample project

comment:3 by blins, 7 years ago

Resolution: needsinfo
Status: closednew

I add project.zip in attachements

comment:4 by Carlton Gibson, 7 years ago

Resolution: invalid
Status: newclosed

See the docs on Reversing namespaced URLs.

Basically you need to include the namespace when using reverse (or the url template tag).

You'll want this:

{%url 'app:all' %}

Also: You've included the app_name in your app/urls.py so your call to include already provides an application_namespace. It's not necessary here to use the namespace parameter as well. (This adds an instance_namespace, which you very likely don't need.)

It's worth reviewing the whole URL Namespaces section slowly, and the `include()` docs to make sure you're clear on the behaviour here.

Note: See TracTickets for help on using tickets.
Back to Top