我正在尝试将Django项目从1.8升级到1.10。以下:Django error: render_to_response() got an unexpected keyword argument 'context_instance'
我从
更改了视图功能from django.shortcuts import render
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.shortcuts import render
from django.core.mail import EmailMessage
from django.http import HttpResponseRedirect
from sellmyland.settings import DEFAULT_FROM_EMAIL
from ipware.ip import get_ip
import json
from myapp.forms import myform
def index(request):
form = myform()
# return render('longform.html', {"form": form}, context_instance=RequestContext(request))
return render(request,'longform.html',RequestContext())
您可以在代码中看到注释掉的版本。我收到上面的错误。这是回溯:
Traceback:
File "....\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "....\lib\site-packages\django\core\handlers\base.py" in _legacy_get_response
249. response = self._get_response(request)
File "....\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "....\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "....myapp\views.py" in index
31. return render(request,'longform.html',RequestContext())
Exception Type: TypeError at /
Exception Value: __init__() missing 1 required positional argument: 'request'
我该如何工作?
编辑:
我尝试同时更改为两者:
return render(request,'longform.html', RequestContext(request, {'form': form}))
和
return render(request,'longform.html', {'form': form})
但是在两种情况下我都会得到:
Template loader postmortem
Django tried loading these templates, in this order:
Using engine :
This engine did not provide a list of tried templates.
Traceback:
File "....\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "....\lib\site-packages\django\core\handlers\base.py" in _legacy_get_response
249. response = self._get_response(request)
File "....\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "....\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\ENVS\r3\sellmyland3\app1\views.py" in index
31. return render(request,'longform.html', RequestContext(request, {'form': form}))
File "....\lib\site-packages\django\shortcuts.py" in render
30. content = loader.render_to_string(template_name, context, request, using=using)
File "....\lib\site-packages\django\template\loader.py" in render_to_string
67. template = get_template(template_name, using=using)
File "....\lib\site-packages\django\template\loader.py" in get_template
25. raise TemplateDoesNotExist(template_name, chain=chain)
Exception Type: TemplateDoesNotExist at /
Exception Value: longform.html
有什么想法吗?
edit2:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(BASE_DIR, 'templates'),
)
edit3:
TypeError:dict最多应有1个参数,得到3个
edit4:
ERRORS:
?: (admin.E402) 'django.contrib.auth.context_processors.auth' must be in TEMPLATES in order to use the admin application.
WARNINGS:
?: (1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your default TEMPLATES dict: TEMPLATE_DEBUG.
System check identified 2 issues (0 silenced).
最后编辑:
我使用以下方法使它起作用:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'debug': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
答案 0 :(得分:3)
问题是RequestContext
类。它具有必填参数request
。但是您无需将RequestContext
作为render
参数来传递。只需使用dict
即可:
return render(request, 'longform.html', {"form": form})
答案 1 :(得分:1)
您没有将request
传递给RequestContext()
,这就是为什么会出错。签出文档here。
def index(request):
form = myform()
return render(request, 'longform.html', RequestContext(request, {'form': form}))
最好使用dict
而不是RequestContext()
。为此,您可以这样做。
def index(request):
form = myform()
return render(request, 'longform.html', {'form': form})
编辑:
将这些添加到您的settings.py
。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
# some options
},
},
]