Home > Net >  Getting TypeError when trying to use Django and Pandas to show data in html
Getting TypeError when trying to use Django and Pandas to show data in html

Time:02-02

import pandas as pd
from django.shortcuts import render

# Create your views here.
def home():
    data = pd.read_csv("\\pandadjangoproject\\nmdata.csv", nrows=11)
    only_city = data[['name']]
    context = {
        "data": data.to_html(index=False),
        "only_city": only_city.to_html()
    }
    return request(render, 'home.html', context)














#Here is my HTML Page
<html>

<head>
    <title>NM Biggest citys</title>
</head>

<body>
    <h1>New Mexicos Largest Citys</h1>

    {{only_city|safe}}
</body>

</html>

#I get this error: TypeError at / home() takes 0 positional arguments but 1 was given Request Method: GET Request URL: http://localhost:8000/ Django Version: 4.0.1 Exception Type: TypeError Exception Value:
home() takes 0 positional arguments but 1 was given

CodePudding user response:

A view function, or view for short, is a Python function that takes a web request and returns a web response.

Add request in your home as

def home(request):
    data = pd.read_csv("\\pandadjangoproject\\nmdata.csv", nrows=11)
    only_city = data[['name']]
    context = {
        "data": data.to_html(index=False),
        "only_city": only_city.to_html()
    }
    return render(request, 'home.html', context)

CodePudding user response:

Looks like the Problem was actually with my csv file it was somehow empty when i copied it from another csv file so i got the original and put it in my django folder with drag and drop. The other problem i did have was in my view I accidently had return request(render but in django its supposed to be return render(request,

  •  Tags:  
  • Related