大家好)我是Django的新手。我需要上传一个.csv文件,验证这是一个csv文件,解析他并使用django.forms保存到数据库。
这是我的模特
class CvsModel(models.Model):
a = models.CharField(max_length=50)
b = models.CharField(max_length=50)
c = models.CharField(max_length=50)
forms.py
from .models import CvsModel
class CvsForm(forms.Form):
file = forms.FileField(label='')
views.py
from .forms import CvsForm
def upload_view(request):
my_form = CvsForm(request.POST or None)
context = {
"form": my_form
}
return render(request, 'upload.html', context)
upload.html
{% extends 'base.html' %}
{% block title %}Upload{% endblock %}
{% block content %}
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
</form>
{% endblock %}