I'm new to Django and need help with views.py. I'm trying to call on a variable from views.py on my HTML template but have no idea how to do so. The following is my views.py function:
def scheduleAlgo(request):
givenData=pd.read_csv('~\OneDrive\Desktop\example.csv')
df = pd.DataFrame(givenData)
df['Mean']=df.mean(axis=1)
df = df.sort_values(by="Mean", ascending=False)
df.set_index("Subject", inplace = True)
firstSubject = df.index[0]
secondSubject = df.index[1]
thirdSubject = df.index[2]
fourthSubject = df.index[3]
fifthSubject = df['Mean'].idxmin()
if fifthSubject==fourthSubject:
fourthSubject=df.index[4]
subjectList=[fifthSubject,fourthSubject,thirdSubject,secondSubject,firstSubject]
subjectSelection = random.choices(subjectList, weights=(20,18,17,16,15),k=5)
return render(request,
'main/testml.html',
{
'firstItem': subjectSelection[0],
'secondItem':subjectSelection[1],
'thirdItem':subjectSelection[2],
'fourthItem':subjectSelection[3],
'fifthItem':subjectSelection[4],
}) #assigning values for calling in template
And this is my HTML code:
<table>
<tr>
<td>{{ firstItem }}</td>
</tr>
</table>
Also, my urls.py:
path("testml", views.scheduleAlgo, name="scheduleAlgo"),
I'm quite sure the function itself works since I tested it out on an iPython notebook.
Essentially, I want to be able to call an index from the list (subjectSelection) and display it in my HTML code. How do I go about doing this?
Well apparently my code was correct but I had assigned two views to a single template. This is incorrect and you have to merge them into a single view in order to correct it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.