我正在尝试将一个简单的csv文件加载到名为class Team
的django模型中</body>
Views.py
class Team(models.Model):
Team = models.CharField(max_length=255,primary_key=True)
Description = models.CharField(max_length=255)
def __str__(self):
return self.Team
HTML
def model_form_upload(request):
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return redirect('home')
else:
form = DocumentForm()
return render(request, 'core/model_form_upload.html', {
'form': form
})
forms.py
{% block content %}
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Upload</button>
</form>
<p><a href="{% url 'home' %}">Return to home</a></p>
{% endblock %}
我创建了一个简单的html页面,将文件加载到以下位置class DocumentForm(forms.ModelForm):
class Meta:
model = Document
fields = ('description', 'document', )
我能够将文件加载到该位置,但我需要一些帮助,将数据传递给实际数据库并将值加载到表“Team”中。有什么建议吗?