Data in 'context' is not passed to HTML - python

The context within the first conditional statement is delivered well, but the context updated within the second conditional statement is not delivered in HTML.
For example, 'MODEL_LIST' in first context is delivered well, but 'mape_val' in second context is not. I want it to be delivered.
How Can I solve this problem ?
<views.py function CODE>
def csv_forecast(request):
context = {}
username = request.user
if request.method == 'POST' and request.FILES.get('csvfile'):
uploaded_file = request.FILES.get('csvfile')
p_data = pd.read_csv(uploaded_file)
p_data.reset_index(drop=True, inplace=True)
columns_list = list(p_data.columns)
columns_list = [column.lower() for column in columns_list]
p_data.columns = columns_list
os.makedirs('media/csv', exist_ok=True)
p_data.to_csv(f'media/csv/{username}.csv', index=False)
start_date = p_data.loc[0, 'date']
len_date = int(len(p_data)*0.8)
end_date = p_data.loc[len_date, 'date']
datas = []
for i in range(1, len(columns_list)):
datas.append(columns_list[i])
MODEL_LIST = ['ARIMA', 'EMA5', 'LSTM']
context = {'datas' : datas, 'd' : p_data, 'columns_list' : columns_list, 'MODEL_LIST' : MODEL_LIST,
'start_date' : start_date, 'end_date' : end_date}
if request.POST.get('sendModel') and request.POST.get('sendPdata') and request.POST.get('sendRdata'):
send_pdata = request.POST.get('sendPdata')
send_rdata = request.POST.get('sendRdata')
send_model = request.POST.get('sendModel')
cleaned_pdata = re.split(r'[\[\],"]', send_pdata)
cleaned_rdata = re.split(r'[\[\],"]', send_rdata)
cleaned_model = re.split(r'[\[\],"]', send_model)
selected_pdata = [i for i in cleaned_pdata if len(i) >= 1]
selected_rdata = [i for i in cleaned_rdata if len(i) >= 1]
selected_model = [i for i in cleaned_model if len(i) >= 1]
csv_data = pd.read_csv(f'media/csv/{username}.csv')
mape_val, y_pred, y_test, test_date = model_main(csv_data, selected_pdata,selected_rdata, selected_model)
mape_val = float(mape_val)
print(mape_val)
fs = FileSystemStorage(location=f'media/csv/')
fs.delete(f'{username}.csv')
context.update({'mape_val':mape_val, 'y_pred':y_pred, 'y_test':y_test, 'test_date':test_date})
return render(request, 'csv_forecast.html', context)
HTML CODE PICTURE
HTML PAGE PICTURE
Debugging
Debug Console

Related

I get a MissingSchema error within my python file that is trying to read a local JSON file

This is the error I get when I try to load my indexes.html page:
MissingSchema at /indexes/
Invalid URL "<_io.TextIOWrapper name='tableInfo.json' mode='r' encoding='cp1252'>": No schema supplied. Perhaps you meant
http://<_io.TextIOWrapper name='tableInfo.json' mode='r'
encoding='cp1252'>?
I am not sure why this is happening, I am trying to read from a local JSON file and display it in a table
This is my views.py code:
def indexes(request):
with open('tableInfo.json') as json_file:
if request.POST:
form = Sea(request.POST)
po = request.POST.get('poNo')
dc = request.POST.get('dcNo')
vendor = request.POST.get('vendor')
order_date = request.POST.get('order_date')
delivery_date = request.POST.get('delivery_date')
payload = {}
if len(po) > 8:
payload['poNo'] = po
if "DC" in dc:
payload['dcNo'] = dc
if len(vendor) > 8:
payload['vendorNo'] = vendor
if len(order_date) > 6:
payload['orderDate'] = order_date
if len(delivery_date) > 6:
payload['deliveryDate'] = delivery_date
data = json.loads((requests.get(json_file, payload)).content)
if data['returnCode'] == 0:
resultList = data['resultList']
else:
resultList = []
else:
form = Sea()
resultList = []
context = {
'data': resultList,
'form': form
}
return render(request, 'users/indexes.html', context)
To read from local file you need
data = json.loads( json_file.read() )
or using load() (without "s" at the end)
data = json.load( json_file )
You don't need requests.get() which makes problem.

Django: update isn't saving the record to the database

def registrar(request):
form = RegForm(request.POST or None)
context = {
"form":form
}
if request.method == 'POST':
form = RegForm(request.POST)
if form.is_valid():
b = form.cleaned_data['folioboleto']
n = form.cleaned_data['nombre']
aP = form.cleaned_data['apellidoPaterno']
aM = form.cleaned_data['apellidoMaterno']
fecha = form.cleaned_data['fechaDeNacimiento']
g = form.cleaned_data['genero']
e = form.cleaned_data['email']
tel = form.cleaned_data['telefono']
di = form.cleaned_data['direccion']
c = form.cleaned_data['ciudad']
est = form.cleaned_data['estado']
actP = form.cleaned_data['actividadPrincipal']
cSE = form.cleaned_data['comoSupoEvento']
aF = form.cleaned_data['aceptoFotos']
obj = Cliente.objects.create(Nombre=n,ApellidoPaterno=aP,ApellidoMaterno=aM,FechaDeNacimiento=fecha,Genero=g,Email=e,Telefono=tel,Direccion=di,Ciudad=c,Estado=est,ActividadPrincipal=actP,ComoSupoEvento=cSE,AceptoFotos=aF)
bole = Boleto.objects.get(Folio=b)
if bole.Folio == b:
bole.Estatus = '2'
bole.Cliente_id = obj.id
bole.save(update_fields=['Estatus'])
The object bole save method isn't saving the changes to the database.
I believe your problem is with bole.save(update_fields=['Estatus']), which would only update the field named Estatus, which doesn't seem to be being set. Try just using a bare save instead:
bole.save()
Good luck!

How to improve time of python code with database queries

I have some database queries in Django 1.11 which takes about 40 seconds and I want to improve the speed, but my ideas are going out.
I want to create a Chart with 6 graphs with the data without having to wait 40 seconds.
This is The Model:
from django.db import models
class MinutenReserveLastYear(models.Model):
datum = models.DateField()
produkt = models.CharField(max_length=100)
grenz_lp = models.FloatField()
mittl_lp = models.FloatField()
class Meta:
indexes = [
models.Index(fields=['datum', 'produkt']),
]
I make an Ajax request to get the Data:
var neg_last_year = $.ajax({
method: "POST",
url: "/prognose/minutenreserve/data/",
data: { "produkt": "NEG", "zeitraum": "last_year" },
dataType: "json",
context: this
});
The view function to get the Data from database:
from .models import MinutenReserveLastYear
def minutenreserve_data(request):
if request.method == "POST":
produkt_group = request.POST.get("produkt")
zeitraum = request.POST.get("zeitraum")
datum_list = {}
grenz_lp = {}
mittl_lp = {}
if zeitraum == "last_year":
if produkt_group == "NEG":
produkt_list = ["NEG_00_04", "NEG_04_08", "NEG_08_12", "NEG_12_16", "NEG_16_20", "NEG_20_24"]
dataset = MinutenReserveLastYear.objects
last_year = datetime.date.fromtimestamp(datetime.datetime.timestamp(
datetime.datetime.now() - datetime.timedelta(weeks=52))).year
days = (datetime.date(last_year, 12, 31) - datetime.date(last_year, 1, 1)).days
for j in range(6):
grenz_lp[j] = []
mittl_lp[j] = []
datum_list[j] = []
produkt = produkt_list[j]
for i in range(days+1):
datum = datetime.date(last_year, 1, 1) + datetime.timedelta(days=i)
datum_list[j] += [datum.strftime('%d.%m.%Y')]
grenz_lp[j] += [dataset.filter(datum=datum, produkt=produkt).values_list()[0][3]]
mittl_lp[j] += [dataset.filter(datum=datum, produkt=produkt).values_list()[0][2]]
data = {'datum_list': datum_list, 'grenz_lp': grenz_lp, 'mittl_lp': mittl_lp}
return JsonResponse(data)
And when the Ajax request is done:
$.when(neg_last_year).done(function(data) {
var datum_list = data.datum_list[0];
var grenzlp_00_04 = data.grenz_lp[0];
var grenzlp_04_08 = data.grenz_lp[1];
var grenzlp_08_12 = data.grenz_lp[2];
var grenzlp_12_16 = data.grenz_lp[3];
var grenzlp_16_20 = data.grenz_lp[4];
var grenzlp_20_24 = data.grenz_lp[5];
var neg_last_year = document.getElementById('NEG_LAST_YEAR').getContext('2d');
var chart_neg_last_year = new Chart(neg_last_year, {
type: 'line',
data: {
labels: datum_list,
datasets: [datasets]
},
options: options
})
});
I suspect that the problem is somewhere in the many database queries through the loops. How can I speed up the code?
How can I reduce the number of database queries?
Edit:
I tried to improve the speed by using
dataset = MinutenReserveLastYear.objects.prefetch_related("grenz_lp", "mittl_lp")
but that did not help.
The main problem is that you are running queries in the loop. What you can do is to get all objects in one query and then filter the objects using if-else in the loops.
Solution:
dataset = MinutenReserveLastYear.objects.all()
for j in range(6):
grenz_lp[j] = []
mittl_lp[j] = []
datum_list[j] = []
produkt = produkt_list[j]
for i in range(days+1):
datum = datetime.date(last_year, 1, 1) + datetime.timedelta(days=i)
for data in dataset:
if data.datum == datum and data.produkt == produkt:
grenz_lp[j].append(data.grenz_lp)
mittl_lp[j].append(data.mittl_lp)

django pass parameters from form to view

I have a form that accepts a single date, or a date range. The date is then passed to a subsequent view that queries the DB and produces a result. I've managed to pass the single date via the URL
but I have yet to find a means to pass the date range from the form to the subsequent view where the query is run.
( Note: I understand that the indentation of the form presented below is off. For some this is my first post and I'm not yet wise to all the subtleties of stack overflow)
class Date_rangeFRM(forms.Form):
Single_date = forms.DateField(required=False)
Date_start = forms.DateField(required=False)
Date_end = forms.DateField(required=False)
def date_range(request):
context = RequestContext(request)
if request.method == 'POST':
form = Date_rangeFRM(request.POST)
print "valid", form.is_valid()
print "form", str(form)
if form.is_valid():
#print "form", str(form)
Single_date = form.cleaned_data['Single_date']
Date_start = form.cleaned_data['Date_start']
Date_end = form.cleaned_data['Date_end']
if Single_date != "":
#print "redirecting to Dav View:", '/counter/%02d-%02d-%02d/Day_view/' %(Single_date.year,Single_date.month,Single_date.day)
return HttpResponseRedirect('/counter/%02d-%02d-%02d/Day_view/' %(Single_date.year,Single_date.month,Single_date.day))
else:
#****passing Magic ********
return HttpResponseRedirect('/counter/History_View/')
else:
print form.errors
else:
form = Date_rangeFRM()
return render_to_response('counter/historyFRM.html', {'form':form}, context)
def History_View(request):
context = RequestContext(request)
#****** Receiving magic********
date_start = form.cleaned_data['date_start']
date_end = form.cleaned_data['date_end']
context['meal_list'] = meal_list
context['comp_list'] = comp_list
context['day_list'] = day_list
dt_range = []
day_list = RecordedDays.objects.filter(profile= request.user.userprofile,
Date__gte = date_start, Date__lte = date_end )
for day in day_list:
dt_range.append(day.Date)
meal_list = MealType.objects.filter(
newDay__profile = request.user.userprofile,
newDay__Date__gte = date_start, newDay__Date__lte = date_end )
#newDay__Date= date( int(year), int(month), int(day) ) )
for meal in meal_list:
dt_range.append([ meal.get_mealNum_display(), meal.Mealtotal() ])
comp_list =Ingredient.objects.filter(numMeal = meal)
for comp in comp_list:
dt_range[-1].append([comp.component, comp.comp_total()] )
return render_to_response('counter/history_view.html', context
Also I'm not entirely sure I'm using the __lte, __gte feature correctly. If you could please inspect it. Much appreciated.
Thanks One and all for your Help.

Django POST/GET exercise

I'm trying to practice some django basics by implementing my own sortable table in Django and I've run into a couple of snags.
Here's the code that I'm working with in my view:
def __table_view_helper__(request):
if not request.session.__contains__('filters'):
filters = {'filterA':'',
'filterB':'',
'filterC':''}
request.session['filters'] = filters
if not request.session.__contains__('headers'):
headers = {'sortA':'asc',
'sortB':'des',
'sortC':'asc',
'sortD':'asc'}
request.session['headers'] = headers
def table_view(request):
__table_view_helper__(request)
records = Record.objects.all()
nums = list(set(records.values_list('fieldA','fieldA')))
nums.sort()
if request.method == 'POST':
filter = FilterForm(nums,request.POST)
if filter.is_valid():
fieldA = filter.cleaned_data['fieldA']
fieldB = filter.cleaned_data['fieldB']
fieldC = filter.cleaned_data['fieldC']
filters = request.session['filters']
filters['fieldA'] = fieldA
filters['fieldB'] = fieldB
filters['fieldC'] = fieldC
request.session['filters'] = filters
else:
filter = FilterForm(nums)
filters = request.session['filters']
filter.fields['fieldA'].initial = filters['fieldA']
filter.fields['fieldB'].initial = filters['fieldB']
filter.fields['fieldC'].initial = filters['fieldC']
if filters['fieldA']:
records = records.filter(fieldA=filters['fieldA'])
if filters['fieldB']:
records = records.filter(fieldB__in=filters['fieldB'])
if filters['fieldC']:
records = records.filter(fieldC=filters['fieldC'])
sort = request.GET.get('sort')
if sort is not None:
headers = request.session['headers']
if headers[sort] == "des":
records = records.order_by(sort).reverse()
headers[sort] = "asc"
else:
records = records.order_by(sort)
headers[sort] = "des"
request.session['headers'] = headers
return render_to_response("secure/table.html",{'user':request.user,'profile':request.user.get_profile(),'records':records,'fform':filter})
I changed a lot of my code to use sessions now. It works fine. Is this a good way to do this you think?
To set the initial values from the view you have to do:
filter.fields['fieldA'].initial = filters['filterA']
To keep user related data persistent through different requests you shouldn't use globals, but sessions!

Categories

Resources