关于django项目中使用python manage.py startapp 应用名
命令出现异常的问题,一般有以下两种情况:
1. 应用名命名不规范
在创建应用时,如果应用名不规范,将会出现异常。在django中,应用名需要遵循以下规则:
- 应用名只能包含字母、数字和下划线;
- 应用名不能以数字开头;
- 应用名不能与已有的django关键字重名,例如:admin, auth, contenttypes 等。
示例:
当我尝试运行以下命令时:python manage.py startapp test-1
。将会看到以下类似的异常信息:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/base.py", line 141, in handle
self.check()
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/base.py", line 371, in check
include_deployment_checks=include_deployment_checks,
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/commands/check.py", line 59, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/checks/registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
return check_method()
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, in check
for pattern in self.url_patterns:
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/urls/resolvers.py", line 579, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/utils/functional.py", line 80, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/urls/resolvers.py", line 572, in urlconf_module
return import_module(self.urlconf_name)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/xxx/test_project/test_project/urls.py", line 22, in <module>
url(r'^', include('test-1.urls')),
ValueError: invalid literal for int() with base 10: '1/'
其中包含ValueError
的异常信息,这是因为应用名中含有-,导致django无法正确地解析应用名,而产生的一个错误。
2. Django项目配置异常
此种异常通常指的是当我们尝试在一个没有初始化为django项目的文件夹中新建一个django项目,并在项目中尝试使用python manage.py startapp 应用名
命令时,会出现异常。这是因为django项目的配置在创建项目时就需要初始化。
示例:
当我尝试在一个无法解析为django项目的文件夹中运行以下命令时:python manage.py startapp test
。将会看到以下类似的异常信息:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/base.py", line 141, in handle
self.check()
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/base.py", line 371, in check
include_deployment_checks=include_deployment_checks,
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/management/commands/check.py", line 59, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/checks/registry.py", line 76, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/core/checks/databases.py", line 10, in check_database_backends
for conn in connections.all():
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/db/utils.py", line 212, in all
return [self[alias] for alias in self]
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/db/utils.py", line 212, in <listcomp>
return [self[alias] for alias in self]
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/db/utils.py", line 187, in __getitem__
conn = backend.DatabaseWrapper(db, alias)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/db/backends/__init__.py", line 181, in __init__
self.ops = ConnectionOps(self)
File "/Users/xxx/.virtualenvs/test/lib/python3.6/site-packages/django/db/backends/mysql/operations.py", line 86, in __init__
super().__init__()
TypeError: super() takes at least 1 argument (0 given)
其中包含了TypeError
的异常信息,这是因为我们尝试在一个没有初始化为django项目的文件夹中尝试新建一个django应用,而产生的一个错误。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:关于django python manage.py startapp 应用名出错异常原因解析 - Python技术站