Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

501
Visualizações
Is it possible to return redirect and return render together in django?

I want to refresh the page after returning a render. I can use return redirect to the same page, but I am confused if I can use both return render and return redirect in the same view function. Is there any other way to reload the page other than redirect without affecting the return render? Or is there a way to re-run the views.text_display() function after each render?

About the project: I'm creating a typing practice program like www.keybr.com

Here is my views.py file:

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 Respostas
Responde à pergunta

0

Yes, a redirect can be used in the same view function. Here is an example

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)

Django redirect method returns a header[302] to the client, requesting a load to a new page. If you don't see the browswr refresh, it's because the browser didn't complete the request.

  • https://docs.djangoproject.com/en/3.2/ref/request-response/#django.http.HttpResponseRedirect
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda