Django redirect() returns 302

I’m trying to make a calculator with user login and sign up functionalities. The calculator is in an app called “calc” and the login/sign up is in another app called “cadastro”. When the user logs in, the view should redirect to the calc:index view, but nothing happens and I get a 302 code in the terminal instead.

This is my cadastro:login view:

def login(request):
    form = formularioLogin()

    if request.method == "POST":
        user = authenticate(username = request.POST.get("username"), password = request.POST.get("password"))

        if user:
            login_django(request, user)

            return redirect("calc:index")

        return JsonResponse({"erro": "Usuário ou senha incorretos."})

    return render(request, "cadastro/login.html", {"form": form})

This is my calc:index view:

@login_required(login_url="cadastro:login")
def index(request):
    historico = calculos.objects.order_by("-pk")

    if request.method == "GET":
        if "apagar" in request.GET:
            apagar = request.GET.get("apagar")

            apagar = calculos.objects.filter(pk=apagar)

            apagar.delete()

            return render(request, "calc/index.html", {"historico": historico})

        elif "conta" in request.GET:
            conta = request.GET.get("conta")

            if re.search("^[0-9.+*\-]+$", conta):
 
                res = str(eval(conta))

                if conta != res:
                    # create = calculos.objects.create(calculo=conta, resultado=res)

                    return render(request, "calc/index.html", {"conta": res, "historico": historico})

                return render(request, "calc/index.html", {"conta": res, "historico": historico})

            return render(request, "calc/index.html", {"erro": "erro", "historico": historico})

        return render(request, "calc/index.html", {"historico": historico})

This is my calc.urls:

app_name = "calc"
urlpatterns = [
    url(r"^$", views.index, name="index"),
]

And this is what I get on my cmd:

[14/Feb/2024 13:36:34] "POST /usuario/login/ HTTP/1.1" 302 0

I’ve tried return redirect(reverse("calc:index")) too, but nothing changed.

I’m using Django 1.11 and python 2.7.

  • Please copy/paste your terminal output of your application logs rather than posting it as a screenshot.

    – 

Leave a Comment