Opened 7 years ago

Closed 7 years ago

#28413 closed Bug (invalid)

app_name doesn't work in ROOT_URLCONF

Reported by: stephanm Owned by: nobody
Component: Core (URLs) Version: 1.11
Severity: Normal Keywords:
Cc: Marten Kenbeek Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In https://docs.djangoproject.com/en/1.11/ref/applications/#projects-and-applications
there is written:
"There’s no restriction that a project package can’t also be considered an application and have models, etc. (which would require adding it to INSTALLED_APPS)"

My environment:

  • django 1.11.3
  • python 3.6.2 64 bit
  • Windows 7 SP1 64bit Pro German

This is just what I did:

  • I created a djproject with django-admin startproject djproject
  • then I added "djproject" to INSTALLED_APPS
  • now I added the following files:
    • djproject/views.py
    • djproject/Templates/djproject/index.html
  • in djproject/urls.py I add the module veriable: app_name = "djproject"


my index.html looks like:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p> the title is {{title}}</p>
<p> the url of this page is: {% url 'index' %}
</body>
</html>

which works!
But if modify it to use namespace 'djproject' like this:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p> the title is {{title}}</p>
<p> the url of this page is: {% url 'djproject:index' %}
</body>
</html>


I get an Exception:

django.urls.exceptions.NoReverseMatch: 'djproject' is not a registered namespace


The whole project is attached. To reproduce:

  • run: manage.py runserver
  • browse to: http://localhost:8000

Attachments (1)

djproject.zip (4.6 KB ) - added by stephanm 7 years ago.
project file to reproduce the error

Download all attachments as: .zip

Change History (6)

by stephanm, 7 years ago

Attachment: djproject.zip added

project file to reproduce the error

comment:1 by Tim Graham, 7 years ago

Cc: Marten Kenbeek added
Component: UncategorizedCore (URLs)
Summary: URL namespaces do not work if the django project is used as django appapp_name doesn't work in ROOT_URLCONF
Type: UncategorizedBug

I think the issue is that app_name isn't supported in ROOT_URLCONF. I'm not sure if it makes sense to support that.

in reply to:  1 comment:2 by Marten Kenbeek, 7 years ago

Resolution: invalid
Status: newclosed

Replying to Tim Graham:

I think the issue is that app_name isn't supported in ROOT_URLCONF. I'm not sure if it makes sense to support that.

That's right, app_name is only supported in included urlconfs.

URL namespaces are designed to prevent naming conflicts between included urlconfs, and to allow the deployment of more than one instance of a specific urlconf. Adding a namespace to the root urlconf only has the cosmetic effect that you need to prepend every viewname with that namespace when reversing, it does not help with naming conflicts or multiple deployments. I don't think it makes sense to support this.

I'm closing this as invalid as it is certainly not a bug.

comment:3 by stephanm, 7 years ago

Resolution: invalid
Status: closednew

OK, now its clear.

Could you ad a small piece of information in:
https://docs.djangoproject.com/en/1.11/ref/applications/#projects-and-applications
in the part:
"There’s no restriction that a project package can’t also be considered an application and have models, etc. (which would require adding it to INSTALLED_APPS)"

Adding that Url namespaces will not work in ROOT_URLCONF and should not be used and are not necessary? (or similar)

I ask for this because the text starts with: There’s no restriction... but in this case we have a restriction.

Thanks.

Last edited 7 years ago by stephanm (previous) (diff)

comment:4 by Tim Graham, 7 years ago

I'm not sure this is a common pitfall -- as Marteen said, why would somebody try to put a namespace in a ROOT_URLCONF? Also, there isn't a restriction that you have to put your ROOT_URLCONF in a "project package" so a note there may be not be most helpful. Rather than a documentation note, it would probably be more helpful to add a system check to error about the mistake but is it worth it? I haven't come across anyone else making the mistake.

comment:5 by Tim Graham, 7 years ago

Resolution: invalid
Status: newclosed

PR to document that namespaces aren't supported in ROOT_URLCONF.

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