我正在尝试使用django为用户创建一个与api交互的站点。网站的第一个页面需要csv上传,然后将数据解析为json格式,并将if off发送到api。
我已经看过投票应用程序的django教程,对django的了解很少。但这真的是压倒性的。
我能够从这个网站上传csv:csv upload
但这会将数据存储到模型中。(这最终可能是好的。)但我在想,我可以直接上传csv并将其直接转换为json,而不存储它?
我想我只是在寻找一些关于如何有一个简单的上传的方向,其中上传按钮只是一个触发csv摄取、格式化和api请求的提交。我认为我唯一需要存储的是来自请求的响应。(它返回一个我需要的ID,以便通过不同的API调用检索结果。)
我还注意到,当前视图仅使用新行更新表。因此,我能够在csv read循环之前添加"profile.objects.all().delete()“,以便在每次上传之前清空该表。
CSV上传:
name,email,address,phone,profile
Sima,simathapa111@gmail.com,Nadipur,14151234567,"She is a developer, researcher and food-lover."
Gita,geeta12@outlook.com,Pokhara,(415) 123-4567,"Geeta is a teacher, she loves to play and dance."
Ram,ram123@gmail.com,Kathmandu,17735356563,Ram delivers food.
JSON格式:
{
"peopleRecords": [
{
"name": "Sima",
"email": "simathapa111@gmail.com",
"address": "Nadipur",
"phone": "14151234567",
"profile": "She is a developer, researcher and food-lover."
},
{
"name": "Gita",
"email": "geeta12@outlook.com",
"address": "Pokhara",
"phone": "(415) 123-4567",
"profile": "Geeta is a teacher, she loves to play and dance."
},
{
"name": "Ram",
"email": "ram123@gmail.com",
"address": "Kathmandu",
"phone": "17735356563",
"profile": "Ram delivers food."
}
]}
型号:
from django.db import models
# Create your models here.
from phonenumber_field.modelfields import PhoneNumberField
class Profile(models.Model):
name = models.CharField(max_length=150)
email = models.EmailField(blank=True)
address = models.CharField(max_length=50)
phone = models.CharField(max_length=150,unique=True)
profile = models.TextField()
def __str__(self):
return self.name
管理员:
from django.contrib import admin
# Register your models here.
from .models import Profile
admin.site.register(Profile)
查看:
import csv, io
from django.shortcuts import render
from django.contrib import messages
# Create your views here.
# one parameter named request
def profile_upload(request):
# declaring template
template = "profile_upload.html"
data = Profile.objects.all()
# prompt is a context variable that can have different values depending on their context
prompt = {
'order': 'Order of the CSV should be name, email, address, phone, profile',
'profiles': data
}
# GET request returns the value of the data with the specified key.
if request.method == "GET":
return render(request, template, prompt)
csv_file = request.FILES['file']
# let's check if it is a csv file
if not csv_file.name.endswith('.csv'):
messages.error(request, 'THIS IS NOT A CSV FILE')
data_set = csv_file.read().decode('UTF-8')
# setup a stream which is when we loop through each line we are able to handle a data in a stream
io_string = io.StringIO(data_set)
next(io_string)
for column in csv.reader(io_string, delimiter=',', quotechar="|"):
_, created = Profile.objects.update_or_create(
name=column[0],
email=column[1],
address=column[2],
phone=column[3],
profile=column[4]
)
context = {}
return render(request, template, context)
模板:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if messages %}
{% for message in messages %}
<div>
<!-- | means OR operator-->
<strong>{{message|safe}}</strong>
</div>
{% endfor %}
{% else %}
{{order}}
<form action="" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<label for="file1"> Upload a file</label>
<input type="file" id="file1" name="file">
<small>Only accepts CSV files</small>
<button type="submit">Upload</button>
</form>
{% endif %}
{% for profile in profiles %}
{{profile.name}}
{% endfor %}
</body>
</html>
URL:
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from csvfile.views import profile_upload
urlpatterns = [
path('admin/', admin.site.urls),
path('upload-csv/', profile_upload, name="profile_upload"),
]
发布于 2021-11-17 11:20:43
可以,您可以将信息放入json中,而无需将其保存到数据库中。但在任何情况下,您都必须保存csv文件,然后处理该文件。保存文件后,您甚至可以使用下面的类似代码执行相同的操作:
csv_file = open("csv_file.csv")
csv_file.readline() # To cross the first line.
data = []
for line in csv_file:
name, email, address, phone, profile = line.split(",")
data.append({
"name": name,
"email": email,
"address": address,
"phone": phone,
"profile": profile
})
# return data ...
https://stackoverflow.com/questions/70008627
复制相似问题