【发布时间】:2016-01-24 07:28:41
【问题描述】:
为了让用户注册我的网站,注册表格需要电子邮件、实际地址、邮政编码和密码。我有两种不同的方式来运行我的应用程序 - 使用 dev_manage.py,它使用 SQLite DB 在 localhost 上运行应用程序,以及使用托管在 EC2 实例上并使用 PostgreSQL DB 的 manage.py。
关键是,当我在本地主机上时,注册表完美地工作,它会在 SQLite DB 中适当地存储电子邮件、地址、邮政编码和密码。但是,在该应用程序的实时 EC2 版本中,我使用了一个 Krispy_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 数据库。
如果这些有用的话……
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')
我完全被难住了。一切如何在 localhost 而不是我的 EC2 实例上完全按预期工作?我是否忘记或错过了某个步骤?另外,请让我知道是否有任何其他 sn-ps 代码有助于解决问题。
【问题讨论】:
标签: python django postgresql amazon-ec2