Python 属性错误:模块';app.management.commands.api';没有属性';命令';
我有一个从API(英国公司)获取信息的文件。为了测试我Python 属性错误:模块';app.management.commands.api';没有属性';命令';,python,django,api,Python,Django,Api,我有一个从API(英国公司)获取信息的文件。为了测试我 import requests r = requests.get('https://api.companieshouse.gov.uk/company/00002065', auth=('xxxxx', '')) print(r.text) 它工作并生成数据,但在数据之后,我得到以下错误消息 AttributeError:模块“app.management.commands.api”没有属性 “命令” 这是为什么?我如何解决 编辑-此外
import requests
r = requests.get('https://api.companieshouse.gov.uk/company/00002065', auth=('xxxxx', ''))
print(r.text)
它工作并生成数据,但在数据之后,我得到以下错误消息
AttributeError:模块“app.management.commands.api”没有属性
“命令”
这是为什么?我如何解决
编辑-此外,如果我在文件所在的同一文件夹中运行“py ch_api.py”,则不会出错;只有在运行“py manage.py ch_scrape”时,我才会得到错误(我需要通过第二种方法运行它)
编辑2-更多信息
档案
我的manage.py文件
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
My settings.py文件
"""
Django settings for project project.
Generated by 'django-admin startproject' using Django 3.0.2.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '*******'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR,'assets'),
)
好的,明白了。来自Rails,我不确定需要什么或为什么命令(BaseCommand),但我会边学边用。如果有人想稍微推动一下这一切,我们将不胜感激
from django.core.management.base import BaseCommand, CommandError
import requests
class Command(BaseCommand):
def handle(self, *args, **options):
r = requests.get('https://api.companieshouse.gov.uk/company/00002065', auth=('DKvKM78qjU0Er_PGYnAdfSpgNquuz9LA_GYp1KNY', ''))
print(r.text)
这里没有足够的信息来帮助您解决此问题-请发布代码,以便有人重现此问题。错误消息表明您的管理命令文件缺少
command
类,这是Django在运行管理命令时查找的。这是文件中的全部代码-我已添加了我的文件夹和文件(如果有帮助)以及“manage.py”和“settings.py”文件,但是,这只是一个新的Django项目,具有“获取”应用程序在管理>命令中添加API文件。如果有人需要任何进一步的信息,请告诉我。同样,实际的函数工作正常,但以我需要修复的错误结束。此外,如果我运行py ch_API.py,它将正常工作;只有当我运行py manage.py ch_scrape时,我才会得到错误(我需要通过第二种方法运行它)建议您阅读有关如何编写管理命令的文档:
from django.core.management.base import BaseCommand, CommandError
import requests
class Command(BaseCommand):
def handle(self, *args, **options):
r = requests.get('https://api.companieshouse.gov.uk/company/00002065', auth=('DKvKM78qjU0Er_PGYnAdfSpgNquuz9LA_GYp1KNY', ''))
print(r.text)