我遇到路由问题django.conf.urls include() - 主项目文件夹 - urls.py
urls.py的第22行(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")),
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
我已经尝试在urls.py中定义app_name =变量,如How to register DRF router url patterns in django 2所示。从Django 1.11移植到2.0后出现问题。 除了通过直接从项目urls.py定义name =“yournametothedetailview”来调用视图之外,还有更好的方法吗?
非常感谢, 最好的恩斯特!
答案 0 :(得分:2)
如果您从旧版本的Django更新,建议您转到特定的应用urls.py
文件并在网址模式上方添加以下行;
app_name = 'current_app_name'
以下是它的外观示例;
app_name = 'current_app_name'
urlpatterns = [
url(r'^$', views.index, name='index'),
]
答案 1 :(得分:0)
我认为能够引用名称空间 - &gt; 在你的urls.py组合应用中你应该设置变量app_name ='你的应用名称'