【问题标题】:AttributeError: 'module' object has no attribute 'META' in Django ProjectAttributeError:“模块”对象在 Django 项目中没有属性“META”
【发布时间】:2015-02-12 02:07:28
【问题描述】:

我正在使用 Python-Django 编写博客。当我在项目文件夹中运行命令“python manage.py runserver”时,出现以下错误:

Traceback (most recent call last):
  File "manage.py", line 6, in <module>
    server = request.META.get('wsgi.file_wrapper', None)
AttributeError: 'module' object has no attribute 'META'

我在任何地方都没有看到这个错误。我应该在我的 installed_apps 中包含 Meta 类吗?我该怎么做?

这是我的 manage.py:

#!/usr/bin/env python
import os
import sys
from django.http import request

server = request.META.get('wsgi.file_wrapper', None)
if server is not None and server.__module__ == 'django.core.servers.basehttp':
    print('inside dev')

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dj1.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

和 settings.py:

"""
Django settings for dj1 project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '(l8*%h2ids25q=x9qb7%x+(b=$1mm&yg8b5ga^0u2w9*+k%+9-'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_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',
    'blog',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'dj1.urls'

WSGI_APPLICATION = 'dj1.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/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/1.7/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,  'templates'),
)

【问题讨论】:

  • 你知道这段代码的初衷是什么吗?据我所知,您想查看运行 django 代码的服务器。如果您仍然想要,或者有其他想法,您可以编辑您的问题,我会尽力提供帮助。

标签: python django


【解决方案1】:

这真是一个大代码。

这里的这段代码在manage.py中没有意义

from django.http import request

server = request.META.get('wsgi.file_wrapper', None)
if server is not None and server.__module__ == 'django.core.servers.basehttp':
    print('inside dev')

也许你从某个地方复制了它,并没有把它放在它所属的地方。

这段代码应该属于一个视图。它永远不会在 manage.py 中工作,并且在那里绝对没有意义。

[编辑] 此外,在一个视图中,您应该包含导入,否则您将得到相同的错误。也就是说,不要包含这个from django.http import request

【讨论】:

    猜你喜欢
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2010-11-18
    相关资源
    最近更新 更多