简体   繁体   中英

__init__() missing 1 required positional argument: 'request'

在此处输入图片说明

I'm trying to upgrade a django project from 1.8 to 1.10. Following: Django error: render_to_response() got an unexpected keyword argument 'context_instance'

I have changed a view function from

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())

You can see the commented out version in the code. I'm getting the error above. Here is the traceback:

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'

How can I get this working?

EDIT:

I tried changing to both:

return render(request,'longform.html', RequestContext(request, {'form': form}))

and

return render(request,'longform.html', {'form': form})

However in both cases I'm getting:

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

Any ideas?

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 expected at most 1 arguments, got 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).

last edit:

I got it working by using:

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

The problem is RequestContext class. It has required argument request . But you don't need to pass RequestContext as render argument. Just use dict instead:

return render(request, 'longform.html', {"form": form})

You are not passing the request to RequestContext() that's why it's giving error. Check out the doc here .

def index(request):

    form = myform()
    return render(request, 'longform.html', RequestContext(request, {'form': form}))

It's better to use a dict rather than RequestContext() . For that you can do this.

def index(request):

    form = myform()
    return render(request, 'longform.html', {'form': form})

EDIT:

Add these to your settings.py .

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            # some options
        },
    },
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM