Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

422
Views
Django ImportError: no se puede importar el nombre 'render_to_response' desde 'django.shortcuts'

Después de actualizar a Django 3.0, aparece el siguiente error:

 ImportError: cannot import name 'render_to_response' from 'django.shortcuts'

Mi vista:

 from django.shortcuts import render_to_response from django.template import RequestContext def index(request): context = {'foo': 'bar'} return render_to_response('index.html', context, context_instance=RequestContext(request))

Aquí está el rastreo completo:

 Traceback (most recent call last): File "./manage.py", line 21, in <module> main() File "./manage.py", line 17, in main execute_from_command_line(sys.argv) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv self.execute(*args, **cmd_options) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 60, in execute super().execute(*args, **options) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute output = self.handle(*args, **options) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 95, in handle self.run(**options) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 102, in run autoreload.run_with_reloader(self.inner_run, **options) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/autoreload.py", line 580, in run_with_reloader start_django(reloader, main_func, *args, **kwargs) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/autoreload.py", line 565, in start_django reloader.run(django_main_thread) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/autoreload.py", line 272, in run get_resolver().urlconf_module File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/urls/resolvers.py", line 572, in urlconf_module return import_module(self.urlconf_name) File "/Users/alasdair/.pyenv/versions/3.7.2/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/Users/alasdair/dev/myproject/myproject/urls.py", line 19, in <module> from myapp import views File "/Users/alasdair/dev/myproject/myapp/views.py", line 8, in <module> from django.shortcuts import render_to_response ImportError: cannot import name 'render_to_response' from 'django.shortcuts' (/Users/alasdair/.virtualenvs/django30/lib/python3.7/site-packages/django/shortcuts.py)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

El atajo render_to_response quedó en desuso en Django 2.0 y se eliminó en Django 3.0 . En su lugar, puede usar el atajo de render , que se agregó en Django 1.3. El atajo de render funciona de manera similar a render_to_response , pero toma la request como su primer argumento. Cambie su vista de la siguiente manera:

 from django.shortcuts import render def index(request): context = {'foo': 'bar'} return render(request, 'index.html', context)

En su opinión, tiene context_instance=RequestContext(request) como tercer argumento. Esto quedó en desuso en Django 1.8 y no funciona en Django 1.10+.

Si está utilizando render_to_response sin context_instance , entonces puede pasar None como la solicitud al atajo de render . Por ejemplo, si tiene,

 return render_to_response('index.html', context)

entonces el equivalente con render es:

 return render(None, 'index.html', context)

Tenga en cuenta que si pasa None como primer argumento, su plantilla se representará sin ningún procesador de contexto . Eso podría hacer que la representación sea un poco más rápida, pero podría generar errores CSRF y significa que no podrá acceder a las variables de los procesadores de contexto (por ejemplo, {{ request }} y {{ user }} ) a menos que agregue explícitamente ellos al contexto. No recomendaría usar None como este a menos que comprenda estas consecuencias.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!