无法覆盖AWS中的Django身份验证模板

时间:2017-07-04 09:47:07

标签: django python-3.x django-authentication elastic-beanstalk

我开发了一个Django应用程序,并且我一直在使用django-authtools模块来使用电子邮件登录。它在我的笔记本电脑上工作得很好但是当我尝试使用Beanstalk在AWS中进行生产时,似乎Django无法识别认证模块的覆盖并且强制重定向到django内置认证模块。其他一切似乎都运行良好(从部署和应用程序的角度来看)。 它导致500错误:

xxx.xxx.xxx.xxx (-) - - [04/Jul/2017:19:07:54 +1000] "GET /accounts/login/ HTTP/1.1" 500 7807 "http://<removed>.ap-southeast-2.elasticbeanstalk.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"

    Internal Server Error: /accounts/login/
    Traceback (most recent call last):
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/core/handlers/exception.py", line 41, in inner
        response = get_response(request)
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/core/handlers/base.py", line 217, in _get_response
        response = self.process_exception_by_middleware(e, request)
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/core/handlers/base.py", line 215, in _get_response
        response = response.render()
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/template/response.py", line 107, in render
        self.content = self.rendered_content
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/template/response.py", line 82, in rendered_content
        template = self.resolve_template(self.template_name)
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/template/response.py", line 64, in resolve_template
        return select_template(template, using=self.using)
      File "/opt/python/run/venv/lib/python3.4/site-packages/django/template/loader.py", line 53, in select_template
        raise TemplateDoesNotExist(', '.join(template_name_list), chain=chain)
    django.template.exceptions.TemplateDoesNotExist: registration/login.html

同样,这在我的笔记本电脑上工作得很好,但在AWS服务器上却没有。我找不到2之间的任何区别,他们正在运行相同版本的django和django-authools:

$ pip freeze

[...]
Django==1.11.2
django-authtools==1.5.0
django-extensions==1.7.6
django-phonenumber-field==1.3.0
django-qsstats-magic==0.7.2
django-simple-captcha==0.5.5
django-storages==1.6.1
[...]

唯一的区别似乎是python的版本,AWS上的3.4.3和笔记本电脑上的3.5.2。

在服务器上,我的settings.py文件与笔记本电脑上的相同:

的myproject / settings.py:

INSTALLED_APPS = [
    'myapp.apps.MyappConfig',
    'authtools',
    'captcha',
    'storages',
    'phonenumber_field',
    'django_extensions',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

AUTH_USER_MODEL = "authtools.User"

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',
            ],
        },

}, ]

的myapp / urls.py:

from authtools import urls
[...]
    url(r'^accounts/', include('authtools.urls')),
[...]

authtools / url.py:

from django.conf.urls import url
from authtools import views as authools_views


urlpatterns = [
    url(r'^login/$', authools_views.LoginView.as_view(), name='login'),
    url(r'^logout/$', authools_views.LogoutView.as_view(), {'next_page': '/accounts/login'}, name='logout'),
    url(r'^password_change/$', authools_views.PasswordChangeView.as_view(), name='password_change'),
    url(r'^password_change/done/$', authools_views.PasswordChangeDoneView.as_view(), name='password_change_done'),
    url(r'^password_reset/$', authools_views.PasswordResetView.as_view(), name='password_reset'),
    url(r'^password_reset/done/$', authools_views.PasswordResetDoneView.as_view(), name='password_reset_done'),
    url(r'^reset/done/$', authools_views.PasswordResetCompleteView.as_view(), name='password_reset_complete'),
    url(r'^reset/(?P<uidb36>[0-9A-Za-z]{1,13})-(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
        authools_views.PasswordResetConfirmView.as_view()),
    url(r'^reset/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
        authools_views.PasswordResetConfirmView.as_view(),
        name='password_reset_confirm'),
]

我还尝试直接在url语句中传递模板(LoginView.as_view(template_name ='myapp / login.html'))但结果相同。

authtools / view.py的摘录:

class LoginView(AuthDecoratorsMixin, WithCurrentSiteMixin, WithNextUrlMixin, FormView):
    print('ENTERING LOGIN VIEW')
    form_class = AuthenticationForm
    template_name = 'myapp/login.html'
    allow_authenticated = True
    success_url = resolve_url_lazy(settings.LOGIN_REDIRECT_URL)

    # BBB: This is deprecated (See LoginView.get_allow_authenticated)
    disallow_authenticated = None

错误“registration / login.html”中显示的模板是内置模板,而不是authtools模块提供的模板,它应该覆盖它。 此外,当导航到我的网站的/ admin /时,我可以与我的超级用户正确登录,当我回到网站时,我被检测为经过身份验证。注销操作虽然将我重定向到默认注销页面,但未使用authtools模块中提供的自定义模板(因此行为相同)。

如果任何人有解决方案或任何调查的地方,我将不胜感激!

谢谢!

我正在添加我的beanstalk配置文件,以防它有所帮助,即使我不相信这是与beanstalk本身相关的问题。

.ebextensions# cat 001_set_env.config 
option_settings:
  "aws:elasticbeanstalk:application:environment":
    PYTHONPATH: /opt/python/current/app/myapp:/opt/python/current/app/authtools:$PYTHONPATH
  "aws:elasticbeanstalk:container:python":
    WSGIPath: "myproject/wsgi.py"
.ebextensions# cat 002_deploy.config 
commands:
  01_update_pip:
    command: "/opt/python/run/venv/bin/pip install --upgrade pip"
  02_set_time_zone:
    command: ln -f -s /usr/share/zoneinfo/Australia/Sydney /etc/localtime

container_commands:
  01_makemigration:
    command: "source /opt/python/run/venv/bin/activate && python manage.py makemigrations --noinput"
    leader_only: true
  02_migrate:
    command: "source /opt/python/run/venv/bin/activate && python manage.py migrate --noinput"
    leader_only: true
  03_initialize_db:
    command: "source /opt/python/run/venv/bin/activate && python manage.py initializedb"
    leader_only: true
  04_create_superuser:
    command: "source /opt/python/run/venv/bin/activate && python manage.py createsu"
    leader_only: true

2 个答案:

答案 0 :(得分:0)

我建议你停止编辑authtools中的代码。目前尚不清楚你是如何做到的,而且由于错误显示缺少的模板是registration/login.html,它现在正在生产中。

如果您将模板从myapp/templates/myapp/login.html重命名为myapp/templates/registration/login.html,则应用程序目录加载程序应找到您的模板。

最后,除此之外,您的DIRS设置看起来不正确。

'DIRS': ['templates'],

如果要包含myproject/templates目录,请将其更改为

'DIRS': [os.path.join(BASE_DIR, 'templates')]

答案 1 :(得分:0)

对于那些感兴趣的人,我终于找到了这个问题的问题。我在site_packages和本地项目中都安装了authools模块。在我的本地笔记本电脑上,似乎优先顺序是首先查看我的本地django项目(我的模块具有正确的模板路径),而在AWS上,python路径顺序首先列出了site_packages,因此查看未修改的版本authtools。 因此我删除了项目中安装的authtools版本,我现在使用site_packages中安装的原始版本,同时直接覆盖我的app urls.py中的authools模板名称。

谢谢大家的帮助,让我走上正轨。

相关问题