当我尝试运行django开发服务器(使用命令行)时,我得到以下错误回溯:

时间:2018-01-24 11:30:16

标签: python django python-3.x

我的系统是:Ubuntu16.04。 Python版本:3.5.2 Django版本:2.0.2

root@nanlyvm:/home/mydj/mysite# python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7fbb791cd1e0>
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/mydj/mysite/mysite/urls.py", line 22, in <module>
    url(r'^blog/',include('blog.urls', namespace='blog', app_name='blog')),
TypeError: include() got an unexpected keyword argument 'app_name'

问题是

url(r'^blog/',include('blog.urls', namespace='blog', app_name='blog')),
TypeError: include() got an unexpected keyword argument 'app_name'

这是我的urls.py,↓我把它存放在/ home / mydj / mysite / mysite

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blog/',include('blog.urls', namespace='blog', app_name='blog')),
]

3 个答案:

答案 0 :(得分:1)

这在Django 1.9中被弃用,在2.0中被删除

https://docs.djangoproject.com/en/2.0/releases/1.9/#urls

  

现在可以使用包含的模块或对象上的app_name属性设置应用程序命名空间。它也可以通过传递( - )的2元组作为include()的第一个参数来设置。

新的包括:https://docs.djangoproject.com/en/2.0/ref/urls/#include

答案 1 :(得分:0)

你必须改变urlpatterns。

from django.conf.urls import url, include
from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blog/',include('blog.urls', name='blog')),
]

参考:https://docs.djangoproject.com/en/2.0/intro/tutorial03/

答案 2 :(得分:0)

根据新版本,请尝试:

from django.urls import include, path

urlpatterns = [
    path('index/', views.index, name='main-view'),
    path('blog', views.blog, name='blog'),
    ...
]

来源:https://docs.djangoproject.com/en/2.0/ref/urls/