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

509
Views
¿Es posible devolver la redirección y devolver el renderizado juntos en django?

Quiero actualizar la página después de devolver un renderizado. Puedo usar la return redirect a la misma página, pero estoy confundido si puedo usar tanto la representación de devolución como la redirección de devolución en la misma función de vista. ¿Hay alguna otra forma de recargar la página que no sea redirigir sin afectar el renderizado de retorno? ¿O hay alguna forma de volver a ejecutar la función views.text_display() después de cada procesamiento?

Sobre el proyecto: Estoy creando un programa de práctica de mecanografía como www.keybr.com

Aquí está mi archivo views.py:

 from django.shortcuts import render import os import keyboard def text_display(request): char_no = request.session.get('char_no', -1) + 1 request.session['char_no'] = char_no f = open(os.path.dirname(os.path.realpath(__file__)) + '\\text1.txt', "r+") file_contents = f.read() if char_no >= len(file_contents): return render(request,'type/main.html',{"key_press":"Test is over"}) for line in file_contents: for ch in line: if keyboard.read_key() == file_contents[char_no]: ctx = {'text': file_contents, 'key_press':'You pressed the right key.'} f.close() return render(request, 'type/main.html', ctx)# Here after returning the render I want to re run this view function. So, reloading looks like a possible thing to do. else: ctx = {'text': file_contents, 'key_press':'You pressed the wrong key.'} return render(request, 'type/main.html', ctx)# Here after returning the render I want to re run this view function. So, reloading looks like a possible thing to do.
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Sí, se puede usar una redirección en la misma función de vista. Aquí hay un ejemplo

 def index(request): ctx = {} # one in 3 chance to redirect if random.randint(0, 2) > 1: return redirect ('/') return render(request, 'type/main.html', ctx)

El método de redirección de Django devuelve un encabezado[302] al cliente, solicitando una carga a una nueva página. Si no ve la actualización del navegador, es porque el navegador no completó la solicitud.

  • https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.HttpResponseRedirect
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!