这是我在postgres的表:
我如何将其引入前端,以便用户可以上传他们的文档,而不必访问数据库。目前,我只能将文档上传到文件系统,而不是数据库:
views.py:
def upload(request):
if request.user.is_authenticated:
username = request.user.username
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage()
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename)
return render(request, 'app/upload.html', {
"uploaded_file_url": uploaded_file_url,
"username": username,
})
return render(request, 'app/upload.html', {
"username": username,
})
upload.html:
{% if form.errors %}
<p>You did not select a file to upload!</p>
{% endif %}
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="myfile">
<button type="submit">Upload</button>
</form>
{% if uploaded_file_url %}
<p>File uploaded at: <a href="{{ uploaded_file_url }}">{{ uploaded_file_url }}</a></p>
{% endif %}
models.py:
class Document(models.Model):
Document_name = models.CharField(max_length=255, default='Document_name')
Date = models.DateField()
Client_id = models.ForeignKey(ClientDetail, on_delete=models.CASCADE)
def __str__(self):
return self.Document_name
结构描述:
还有我将如何检索上传的文档。
1条答案
按热度按时间b1uwtaje1#
以下是一个示例:
settings.py
urls.py
models.py
forms.py
upload.html:
views.py