Python __init_u3;()缺少1个必需的位置参数:“请求”
我正在尝试将django项目从1.8升级到1.10。以下: 我已将视图功能从Python __init_u3;()缺少1个必需的位置参数:“请求”,python,django,Python,Django,我正在尝试将django项目从1.8升级到1.10。以下: 我已将视图功能从 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 djan
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}))
及
然而,在这两种情况下,我得到:
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
有什么想法吗
编辑2:
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'),
)
编辑3:
TypeError:dict最多需要1个参数,得到3个
编辑4:
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',
],
},
},
]
问题是RequestContext类。它需要参数请求。但不需要将RequestContext作为呈现参数传递。只需使用dict即可:
问题是RequestContext类。它需要参数请求。但不需要将RequestContext作为呈现参数传递。只需使用dict即可:
您没有将请求传递给RequestContext,这就是它给出错误的原因。查看文档
最好使用dict而不是RequestContext。为此,你可以这样做
def index(request):
form = myform()
return render(request, 'longform.html', {'form': form})
编辑:
将这些添加到settings.py
您没有将请求传递给RequestContext,这就是它给出错误的原因。查看文档
最好使用dict而不是RequestContext。为此,你可以这样做
def index(request):
form = myform()
return render(request, 'longform.html', {'form': form})
编辑:
将这些添加到settings.py
值得更新答案,以解释RequestContext是它抱怨没有向其传递请求对象的原因。@user61629您的模板在哪里?您应该将模板目录添加到模板的目录设置中:值得更新答案,以解释RequestContext之所以抱怨没有向其传递请求对象。@user61629您的模板位于哪里?您应该将模板目录添加到模板的目录设置:@user61629在settings.py中显示您的模板设置。是否已将模板添加到目录?错误:?:admin.E402“django.contrib.auth.context_processors.auth”必须位于模板中才能使用管理应用程序。警告:?:1_8.W001 Django 1.8中不推荐独立模板设置,模板字典优先。必须将以下设置的值放入默认模板dict:TEMPLATE\U DEBUG。系统检查发现2个问题0已静音。@user61629从选项中删除上下文处理器,我已更新我的答案。并传递一个dict来呈现,而不是RequestContext。@user61629在settings.py中显示您的模板设置。是否已将模板添加到目录?错误:?:admin.E402“django.contrib.auth.context_processors.auth”必须位于模板中才能使用管理应用程序。警告:?:1_8.W001 Django 1.8中不推荐独立模板设置,模板字典优先。必须将以下设置的值放入默认模板dict:TEMPLATE\U DEBUG。系统检查发现2个问题0已静音。@user61629从选项中删除上下文处理器,我已更新我的答案。并将dict传递给render,而不是RequestContext。
def index(request):
form = myform()
return render(request, 'longform.html', RequestContext(request, {'form': form}))
def index(request):
form = myform()
return render(request, 'longform.html', {'form': form})
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
# some options
},
},
]