Skip to content

Instantly share code, notes, and snippets.

@richardoey
Last active December 7, 2020 09:32
Show Gist options
  • Save richardoey/44c5ef690d5d9c796f876fb257098344 to your computer and use it in GitHub Desktop.
Save richardoey/44c5ef690d5d9c796f876fb257098344 to your computer and use it in GitHub Desktop.
read csv file data in Django
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<meta name="viewport" content="width=device-width" />
<title>ATE-Dashboard</title>
</head>
<body>
<div class="row">
<div class="content">
<h1 class="content-title">
{{ list }}
</h1>
</div>
</div>
</body>
</html>
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from django.contrib.staticfiles.storage import staticfiles_storage
import csv
def index(request):
template = loader.get_template('dashboard/index.html')
path = staticfiles_storage.path('setting/dashboard/Coreboard_test.csv')
csv_rows = []
with open(path) as f:
reader = csv.reader(f)
for i, row in enumerate(reader, start=0):
csv_rows.append([])
for j in range(len(row)):
csv_rows[i].append(row[j])
context = {
'title': "Coreboard",
'header_list' : csv_rows[0],
'data_list' : csv_rows[1:]
}
return HttpResponse(template.render(context, request))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment