我有兴趣学习Django for python。因此,我按照他们的教程 - 但现在我被困在第二个教程元素的以下点:
问题:根据教程,我将使用以下代码运行“数据库迁移”:
python3 manage.py makemigrations
python3 manage.py migratecode
我在执行第一个命令(makemigrations)时收到以下错误消息:
> (django_second_test) sebastian@sebastian-Lenovo-Y50-70
> ~/Dokumente/py_virtualenv/django_virtualenv/django_second_test/locallibrary/locallibrary
> $ python3 manage.py makemigrations Traceback (most recent call last):
> File "manage.py", line 15, in <module>
> execute_from_command_line(sys.argv) File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
> utility.execute() File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/core/management/__init__.py", line 365, in execute
> self.fetch_command(subcommand).run_from_argv(self.argv) File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/core/management/base.py",
> line 288, in run_from_argv
> self.execute(*args, **cmd_options) File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/core/management/base.py",
> line 332, in execute
> self.check() File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/core/management/base.py",
> line 364, in check
> include_deployment_checks=include_deployment_checks, File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/core/management/base.py",
> line 351, in _run_checks
> return checks.run_checks(**kwargs) File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/core/checks/registry.py",
> line 73, in run_checks
> new_errors = check(app_configs=app_configs) File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/core/checks/urls.py",
> line 40, in check_url_namespaces_unique
> all_namespaces = _load_all_namespaces(resolver) File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/core/checks/urls.py",
> line 57, in _load_all_namespaces
> url_patterns = getattr(resolver, 'url_patterns', []) File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/utils/functional.py",
> line 36, in __get__
> res = instance.__dict__[self.name] = self.func(instance) File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/urls/resolvers.py",
> line 536, in url_patterns
> patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) File
> "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-packages/django/utils/functional.py",
> line 36, in __get__
> res = instance.__dict__[self.name] = self.func(instance) File "/home/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/lib/python3.5/site-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/sebastian/Dokumente/py_virtualenv/django_virtualenv/django_second_test/locallibrary/locallibrary/locallibrary/urls.py",
> line 20, in <module>
> url(r'^admin/', admin.site.urls), NameError: name 'url' is not defined
我已经重新检查了我的代码,甚至根据教程规范copy_pasted - 但作为一个初学者(也在Python中)我很难理解发生了什么。
链接到教程页面:Link to second part of Django Tutorial
其他信息:虚拟环境处于活动状态(通过“source activate”激活); Python3.5在虚拟环境中。 tutoriual的第一部分(虚拟环境和python的设置以及通过pip3安装django是成功的)。我在薄荷linux上。
感谢大家的帮助!
塞巴斯蒂安
您正在获取NameError
,因为您缺少以下导入:
from django.conf.urls import url
这是因为您使用Django 2.0创建了项目,但该教程是为Django 1.11编写的。你得到NameError
,因为Django 2.0中的默认urls.py
导入path()
而不是url()
:
from django.urls import path
我建议您使用Django 2.0和official tutorial for Django 2.0,或者使用Django 1.11的Mozilla教程。如果您的Django版本与编写该教程的版本不匹配,那么您更有可能遇到类似这样的问题,当您刚接触Django时可能会感到沮丧。
如果你坚持使用Mozilla教程,你可以安装Django 1.11的最新版本:
pip3 install "django<2"