render()调用中的Django TypeError一旦上传到服务器

时间:2017-05-12 00:24:38

标签: python django python-2.7 django-templates django-views

我有一个网站,我正在更新到最新的Django,一次发布一个点。现在我在Django 1.9.13和Python 2.7.5上,我的代码在我的本地开发机器上与manage.py runserver一起工作正常,但是当我运行它到我的web主机(WebFaction)时,它失败并出现此错误:

   / _的TypeError    init ()只需要3个参数(给定2个)

这里是追溯:

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  149.                     response = self.process_exception_by_middleware(e, request)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  147.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/myuser/webapps/optical_test/cinedex/theoptical/views.py" in frontpage
  38.     return render(request, 'theoptical/frontpage.html', context)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/shortcuts.py" in render
  67.             template_name, context, request=request, using=using)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  96.             template = get_template(template_name, using=using)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/template/loader.py" in get_template
  32.                 return engine.get_template(template_name, dirs)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/template/backends/django.py" in get_template
  40.             return Template(self.engine.get_template(template_name, dirs), self)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/template/engine.py" in get_template
  190.         template, origin = self.find_template(template_name, dirs)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/template/engine.py" in find_template
  153.         for loader in self.template_loaders:

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/utils/functional.py" in __get__
  33.         res = instance.__dict__[self.name] = self.func(instance)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/template/engine.py" in template_loaders
  118.         return self.get_template_loaders(self.loaders)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/template/engine.py" in get_template_loaders
  123.             loader = self.find_template_loader(template_loader)

File "/home/myuser/.virtualenvs/optical_test/lib/python2.7/site-packages/django/template/engine.py" in find_template_loader
  146.             return loader_class(*args)

Exception Type: TypeError at /
Exception Value: __init__() takes exactly 3 arguments (2 given)

此处cinedex/theoptical/views.py中的frontpage

def frontpage(request):
    context = {'title':"The Optical Test New Front Page"}

    # Don't return episodes with a pubdate in the future
    now = timezone.now()
    if Episode.objects.filter(pubDate__lt=now):
        latest_episode = get_object_or_404(Episode.objects.filter(pubDate__lt=now).order_by('-pubDate')[:1])
        context.update({'latest_episode': latest_episode})

    context.update(masterlinks())
    return render(request, 'theoptical/frontpage.html', context)

有一个建议我应该在哪里查找错误?让我知道如果提供更多信息对我有用。谢谢!

更新

以下是我settings.py

的模板设置
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
        ],
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
            'loaders': [
                'django.template.loaders.cached.Loader',
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ],
            'debug':True,
        },
    },
]

2 个答案:

答案 0 :(得分:2)

查看模板设置似乎django.template.loaders.cached.Loader应该设置不同(docs):

TEMPLATES = [{
    # ...
    'OPTIONS': {
        'loaders': [
            ('django.template.loaders.cached.Loader', [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
            ]),
        ],
    },
}]

答案 1 :(得分:-2)

您的错误来自此行

latest_episode = get_object_or_404(Episode.objects.filter(pubDate__lt=now).order_by('-pubDate')[:1])

您错过了一个参数 - 请参阅docs。你应该做的事情如下:

eps = get_object_or_404(Episode, pubDate__lt=now)
latest_episode = eps.order_by('-pubDate')