为了让用户注册我的网站,注册表需要电子邮件,实际地址,邮政编码和密码。我有两种不同的方式来运行我的应用程序 - 使用dev_manage.py,它使用SQLite数据库在localhost上运行应用程序,以及manage.py,它托管在EC2实例上并使用PostgreSQL数据库。
问题是,当我在我的本地主机上时,注册表格完美,它会在SQLite DB中正确存储电子邮件,地址,邮政编码和密码。但是,在应用程序的实时EC2版本中,使用crispy_forms帮助程序时出现VariableDoesNotExist错误,由于某种原因,该错误试图仅需要电子邮件和密码。另一方面,帮助程序布局试图强制使用电子邮件,地址,邮政编码和密码。另外,如果我删除了帮助'从模板标签,然后localhost注册表仍然要求提供电子邮件,地址,邮政编码和密码,而EC2注册表不再给我一个VariableDoesNotExist错误,但只询问电子邮件和密码。
有趣的是,dev.py和settings.py之间的唯一区别是每个版本使用的数据库:
dev.py (在dev_manage.py中调用):
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import logging, os
import logging.handlers
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/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'XXXXXXXXXXX'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1:8000']
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_HOST = 'localhost'
EMAIL_PORT = 1025
EMAIL_USE_TLS = True
# Application definition
INSTALLED_APPS = (
# Django Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',
# My Apps
'cart',
'recipes',
'djangoratings',
'registration',
'customer_profile',
'stripe',
# Third Party Apps
'crispy_forms',
)
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',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'foodshop.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "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 = 'foodshop.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/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.8/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.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static_root")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static_in_pro", "our_static"),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media_root")
CRISPY_TEMPLATE_PACK = 'bootstrap3'
#DJANGO REGISTRATION REDUX SETTINGS
ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_AUTO_LOGIN = True
SITE_ID = 1
LOGIN_REDIRECT_URL = '/menu'
AUTH_USER_MODEL = 'customer_profile.MyUser'
STRIPE_SECRET_KEY = "XXXXXXXXXXX"
settings.py (在manage.py中调用):
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import logging, os
import logging.handlers
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/1.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'XXXXXXXXXX'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'XXXXXXXXXX'
EMAIL_HOST_PASSWORD = 'XXXXXXXXXX'
# Application definition
INSTALLED_APPS = (
# Django Apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.sites',
'django.contrib.staticfiles',
# My Apps
'cart',
'recipes',
'djangoratings',
'registration',
'customer_profile',
'stripe',
# Third Party Apps
'crispy_forms',
)
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',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'foodshop.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, "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 = 'foodshop.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'XXXXXXXXXX',
'USER' : 'XXXXXXXXXX',
'PASSWORD' : 'XXXXXXXXXX',
'HOST' : 'XXXXXXXXXX.us-west-2.rds.amazonaws.com',
'PORT' : '5432',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/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.8/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static_root")
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static_in_pro", "our_static"),
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media_root")
CRISPY_TEMPLATE_PACK = 'bootstrap3'
#DJANGO REGISTRATION REDUX SETTINGS
ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_AUTO_LOGIN = True
SITE_ID = 1
LOGIN_REDIRECT_URL = '/menu'
AUTH_USER_MODEL = 'customer_profile.MyUser'
STRIPE_SECRET_KEY = "XXXXXXXXXX"
此外,我已经删除了架构,删除了所有迁移并重新迁移 - 基本上从头开始。存在必要的表和列,模式看起来正确,并且我连接到PostgreSQL DB。
如果这些有用......
registration.forms.py
from __future__ import unicode_literals
from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field
from .users import UserModel, UsernameField
User = UserModel()
class RegistrationForm(UserCreationForm):
"""
Form for registering a new user account.
Validates that the requested username is not already in use, and
requires the password to be entered twice to catch typos.
Subclasses should feel free to add any additional validation they
need, but should avoid defining a ``save()`` method -- the actual
saving of collected user data is delegated to the active
registration backend.
"""
email = forms.EmailField(required=True, label='E-mail')
address = forms.CharField(required=True, label='Street Address')
zip_code = forms.CharField(required=True, label='Zip Code')
class Meta:
model = User
fields = ('email', 'address', 'zip_code', 'password1', 'password2')
def __init__(self, *args, **kwargs):
super(RegistrationForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_show_labels = False
self.helper.form_id = 'id-exampleForm'
self.helper.form_class = 'blueForms'
self.helper.form_method = 'post'
self.helper.form_action = 'submit_survey'
self.helper.add_input(Submit('submit', 'Create Account', css_class='btn btn-lg btn-primary'))
self.helper.layout = Layout(
Field('email', placeholder='E-mail'),
Field('address', placeholder='Street Address'),
Field('zip_code', placeholder='Zip Code'),
Field('password1', placeholder='Password'),
Field('password2', placeholder='Confirm Password'),
)
registration.users.py
from django.conf import settings
from .compat import get_model
try:
from django.contrib.auth import get_user_model
UserModel = get_user_model
except ImportError:
UserModel = lambda: get_model('auth', 'User')
def UserModelString():
try:
return settings.AUTH_USER_MODEL
except AttributeError:
return 'auth.User'
def UsernameField():
return getattr(UserModel(), 'USERNAME_FIELD', 'email')
我完全难过了。一切如何能够在本地主机上完全按预期工作,而不是在我的EC2实例上工作?我忘了或错过了某一步吗?另外,如果有任何其他代码段可用于帮助排除故障,请告诉我。
答案 0 :(得分:0)
经过几天仔细检查我的代码后,我才发现根本原因。我安装了django-registration-redux,并且做了pip uninstall django-registration-redux修复了这个问题。我不完全确定导致问题的技术原因,但同样的问题也影响了我构建的其他应用程序。