У меня проблема с маршрутизацией django.conf.urls include () - основная папка проекта - urls.py

Строка 22 urls.py (pip freeze и urls.py - см. Ниже) выдает ошибку в консоли:

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")),

консоль $ 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

Я уже пробовал определить переменную app_name = в urls.py, как предлагается здесь Как зарегистрировать шаблоны URL-адресов маршрутизатора DRF в django 2. Проблема возникла после переноса с Django 1.11 на 2.0. Есть ли лучший способ, кроме вызова представления, путем определения name = "yournametothedetailview" непосредственно из проекта urls.py.

Большое спасибо, лучший Эрнст!

0
Ernst Plesiutschnig 2 Янв 2018 в 18:43

2 ответа

Лучший ответ

На всякий случай, если вы обновили более старую версию Django, было бы желательно, если вы зайдете в конкретный файл приложения urls.py и добавите следующую строку прямо над шаблонами URL-адресов;

app_name = 'current_app_name'

Ниже приведен пример того, как это будет выглядеть;

app_name = 'current_app_name'

urlpatterns = [
     url(r'^$', views.index, name='index'),
]
2
Muema 21 Янв 2018 в 19:12

Я думаю, чтобы иметь возможность ссылаться на пространство имен → в вашем urls.py приложения композиции, вы должны установить переменную app_name = 'your app name'

0
DisplayName 2 Янв 2018 в 19:58