0

我是 Django 新手,所以通常我是大多数问题的原因,但我不知道为什么 django-guardian 1.3 应用程序无法安装。我在虚拟环境中使用 Django 1.7,我的操作系统是 Windows 8.1。

我遵循了 pythonhosted.org/django-guardian/installation.html 上的安装说明和pythonhosted.org/django-guardian/configuration.html上的配置,但是当我尝试运行程序时出现错误。

我在我的 settings.py 中添加了“监护人”、ANONYMOUS_USER_ID 和后端

"""
Django settings for VolunteerManager 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 = 'Super Super Secret'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []
ANONYMOUS_USER_ID = -1

# Application definition

INSTALLED_APPS = (
#DEFAULT APPS
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

#THIRD PARTY APPS
    'guardian',
    'registration',
        #Copyright (c) 2007-2012, James Bennett
        #All rights reserved.
    'django.contrib.sites',

#LOCAL APPS
    'Volunteer',
)

ACCOUNT_ACTIVATION_DAYS = 7 # One-week activation window;
REGISTRATION_AUTO_LOGIN = True # Automatically log the user in.

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

AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend', 
    'guardian.backends.ObjectPermissionBackend',
)

ROOT_URLCONF = 'VolunteerManager.urls'


#ANONYMOUS_USER_ID = 'VOLUNTEER_USER_ID'

WSGI_APPLICATION = 'VolunteerManager.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'volunteer',
        'USER': 'root',
        'PASSWORD': '$',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

# 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')]

LOGIN_REDIRECT_URL = '/home/'

SITE_ID = 1

IMGUR 上提供的错误图片

Django-guardian 似乎已安装在我的虚拟环境中,但仍未找到。任何想法我可能做错了什么?(或其他关于 Django 中每个对象权限的建议?)谢谢!

更新:我将问题缩小到 virtualenv。当我在不使用 virtualenv 的情况下安装模块时,django 会像它应该的那样找到它们。我仍然不确定我到底做错了什么,但考虑到我目前只在做一个项目,这暂时有效。

4

2 回答 2

0

我将问题缩小到 virtualenv。当我在不使用 virtualenv 的情况下安装模块时,django 会像它应该的那样找到它们。

每次运行 django 时都需要激活虚拟环境,否则您将继续遇到您所描述的问题。

激活虚拟环境(通过执行Scripts\activate.bat)将正确的环境变量设置为所有 Python 命令都针对虚拟环境的 Python 安装运行。

如果在运行 Python 命令之前不运行该activate.bat文件,它们将针对系统范围的 Python 安装执行。

django 工作的原因是因为您在没有激活 virtualenv 的情况下运行 django;并且您还碰巧在全局 Python 环境中安装了 django。

于 2015-06-28T08:30:27.050 回答
0

对于遇到同样问题的人:

原因可能是您不拥有 virtualenv 文件夹及其里面的所有内容,因此

sudo chown -R /path/to/venv

应该管用。

于 2017-07-30T16:59:52.847 回答