django 2.0 url.py include namespace=“xyz”

2019-08-21 01:17发布

问题:

I've a problem with the routing django.conf.urls include() - main project folder - urls.py

Line 22 of urls.py (pip freeze and urls.py - see below) throws the error in the console:

Quit the server with CONTROL-C.
[02/Jan/2018 14:22:49] "GET /api/compositions/ HTTP/1.1" 200 30058
[02/Jan/2018 14:22:53] "GET /api/compositions/1/ HTTP/1.1" 200 6195
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f17ee06e400>
Traceback (most recent call last):
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/home/ernst/django_virtualenv/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/ernst/django_virtualenv/mwrench/mwrench/urls.py", line 23, in <module>
    url(r'^api/compositions/', include("compositions.api.urls", namespace="compositions-api")),
  File "/home/ernst/django_virtualenv/lib/python3.4/site-packages/django/urls/conf.py", line 39, in include
    'Specifying a namespace in include() without providing an app_name '
django.core.exceptions.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.

urls.py:

 from compositions.api.views import AlloyelementDetailAPIView                                                                                                                            
  1 # Without settings the API URL queries do not work                              
  2 from django.conf.urls.static import static                                      
  3 from django.conf import settings                                                                                        
  4 from django.conf.urls import include, url                                       
  5 from django.contrib import admin                                                                                        
  6 from mwrench.views import (                                                                                             
  7         template,                                                                                                       
  8         ChartData,                                                                                                      
  9         CompositionAPI,                                                                                                 
 10         testview                                                                                                        
 11         )                                                                                                               
 12 from rest_framework.urlpatterns import format_suffix_patterns                   
 13 from accounts.views import (login_view, register_view, logout_view)              
 14                                                                                                                         
 15 urlpatterns = [                                                                                                         
 16     url(r'^admin/', admin.site.urls),                                                                                   
 17     url(r'^', include('compositions.urls' )),                                   

...

 22     url(r'^api/compositions/', include("compositions.api.urls", namespace="compositions-api")),

console$ pip freeze

Django==2.0
Jinja2==2.10
Markdown==2.6.10
MarkupSafe==1.0
PyYAML==3.12
appdirs==1.4.3
certifi==2017.11.5
chardet==3.0.4
deprecation==1.0.1
django-braces==1.12.0
django-crispy-forms==1.7.0
django-filter==1.1.0
djangorestframework==3.7.7
gunicorn==19.7.1
idna==2.6
include==0.2.1
iso8601==0.1.12
jsonpatch==1.21
jsonpointer==1.14
keystoneauth1==3.3.0
mysqlclient==1.3.12
numpy==1.13.3
openstacksdk==0.9.19
os-client-config==1.28.0
pbr==3.1.1
psycopg2==2.7.3.2
pytz==2017.3
requests==2.18.4
requestsexceptions==1.3.0
reverse==0.1.0
six==1.11.0
stevedore==1.28.0
url==0.4.2
urllib3==1.22

I already tried defining the app_name= variable within the urls.py, as suggested here How to register DRF router url patterns in django 2. The problem occured after porting from Django 1.11 to 2.0. Is there a better way except calling the view by defining the name="yournametothedetailview" directly from the project urls.py.

Thank you very much, best Ernst!

回答1:

Just in case you have updated from an older version of Django it would be advisable if you go to the specific app urls.py file and add the following line just above the url patterns;

app_name = 'current_app_name'

Below is an example of how it will look like;

app_name = 'current_app_name'

urlpatterns = [
     url(r'^$', views.index, name='index'),
]


回答2:

i think to be able to reference a namespace -> in your urls.py of composition app you're supposed to set the variable app_name = 'your app name'