Hello I’m trying to redirect after a successful post to another page within the same app and its not working. Whenever I use the developer tools in the browser I can see a preview of the desired template with the correct data and the correct url is being generated in a GET request, if I input it manually it works. I’ve been stuck on this for a day now and would appreciate any help, Thank you.
# This is my views.py
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.template import loader
from django.shortcuts import get_object_or_404, render, redirect
from django.urls import reverse
import plotly.graph_objs as go
import numpy as np
import json
import requests
# Create your views here.
def solicitud(request):
if request.method == 'GET':
url = "http://127.0.0.1:8080/API/"
response = requests.get(url)
#print(response.content)
#print(response.headers)
if response.status_code == 200:
posibles_valores = response.json()
#print(posibles_valores)
genero = posibles_valores["posibles_valores"]["Genero"]
ocupacion = posibles_valores["posibles_valores"]["Ocupacion"]
empresa = posibles_valores["posibles_valores"]["Empresa"]
antiguedad = posibles_valores["posibles_valores"]["Antiguedad"]
estado = posibles_valores["posibles_valores"]["Estado"]
estado_civil = posibles_valores["posibles_valores"]["Estado civil"]
context = {
'genero': genero,
'ocupacion':ocupacion,
'empresa':empresa,
'antiguedad':antiguedad,
'estado': estado,
'estado_civil': estado_civil
}
return render(request, 'components/forms/form-captura_datos_credito.html', context)
else:
error_msg = f"Error {response.status_code}: {response.reason}"
return HttpResponse(json.dumps({"error": error_msg}), content_type="application/json", status=response.status_code)
elif request.method == "POST":
url = "http://127.0.0.1:8080/API/" # The URL of your Django REST API endpoint
headers = {'content-type': 'application/json'}
data = {
"Plazo en meses": int(request.POST.get("plazo-en-meses")),
"Edad": int(request.POST.get("edad")),
"Numero de contratos previos": int(request.POST.get("contratos-previos")),
"Ingreso mensual": int(request.POST.get("ingreso-mensual")),
"% de pago/ingreso": 0.1,
"Antiguedad": request.POST.get("selector-antiguedad"),
"Ocupacion": request.POST.get("ocupacion-datalist"),
"Empresa": request.POST.get("empresa-datalist"),
"Estado": request.POST.get("estado-datalist"),
"Estado civil": request.POST.get("selector-estado-civil"),
"Genero": request.POST.get("selector-genero"),
} # The data you want to send in the POST request
response = requests.post(url, headers=headers, json=data)
#print(response.content)
if response.status_code == 200:
# Success! The API returned a response
#new_resource_url = response.headers["Location"] # The URL of the newly created resource
new_resource_data = response.content.decode() # The data of the newly created resource in JSON format
##########
new_resource_data = json.loads(new_resource_data)
new_resource_data['etiquetas'][8] = "pago ingreso"
new_resource_data = json.dumps(new_resource_data, separators=(',',':'))
##########
#print(new_resource_data)
new_resource_data = json.dumps(new_resource_data, separators=(',', ':'))
#print(new_resource_data)
url = reverse('calificacion', kwargs={'response_data' : new_resource_data})
#url = request.build_absolute_uri(reverse('calificacion', kwargs={'response_data' : new_resource_data}))
return HttpResponseRedirect(url)
else:
# Oops, there was an error. Handle it here
error_message = f"Error {response.status_code}: {response.reason}"
return HttpResponse(json.dumps({"error": error_message}), content_type="application/json", status=response.status_code)
def calificacion(request, **kwargs):
if request.method == "GET":
response_data = json.loads(kwargs['response_data'])
#print(response_data)
'''
print(type(response_data))
trace = go.Waterfall(
name="Calificacion", orientation="h",
measure=["relative", "relative", "relative", "relative", "relative", "relative","relative", "relative", "relative", "relative", "relative", "relative","total"],
y=response_data["etiquetas"],
x=response_data,
connector = {"mode":"between", "line":{"width":4, "color":"rgb(0, 0, 0)", "dash":"solid"}},
)
layout = go.layout(
title='Calificacion',
showlegend = True
)
fig = go.Figure(data=[trace], layout=layout)
graph = fig.to_html(full_html=False)
#print(response_data)
'''
return render(request, 'components/charts/charts-waterfall.html', {'response_data': response_data})
else:
None```
# This is my urls.py
from django.urls import path
from . import views
urlpatterns=[
path('solicitud/', views.solicitud, name='solicitud'),
path('calificacion/<str:response_data>/', views.calificacion, name='calificacion'),
]```