【问题标题】:Getting TypeError when trying to use Django and Pandas to show data in html尝试使用 Django 和 Pandas 在 html 中显示数据时出现 TypeError
【发布时间】:2022-02-02 03:18:45
【问题描述】:
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>

#我得到这个错误: 类型错误在 / home() 接受 0 个位置参数,但给出了 1 个 请求方法:GET 请求网址:http://localhost:8000/ Django 版本:4.0.1 异常类型:TypeError 异常值:
home() 接受 0 个位置参数,但给出了 1 个

【问题讨论】:

  • 你怎么打电话给home()

标签: python html django pandas


【解决方案1】:

视图函数,或简称视图,是一个接受网络请求并返回网络响应的 Python 函数。

request 添加到您的home

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)

【讨论】:

    【解决方案2】:

    看起来问题实际上出在我的 csv 文件上,当我从另一个 csv 文件复制它时它是空的,所以我得到了原始文件并将它拖放到我的 django 文件夹中。 我确实遇到的另一个问题是我不小心有返回请求(渲染但在 django 中它应该是返回渲染(请求,

    【讨论】:

      猜你喜欢
      • 2016-09-04
      • 2020-10-29
      • 2021-10-14
      • 2021-05-24
      • 2021-09-05
      • 2022-10-21
      • 2021-12-13
      • 1970-01-01
      • 2021-08-05
      相关资源
      最近更新 更多