首页 > 解决方案 > 如何使用 django 中的 filesystemstorage 从上传的文件中提取数据?

问题描述

你好我正在学习python

我尝试使用 FileSystemStorage defualt 类上传文件,该类是 django 中的默认类,文件正在上传,但我想从上传的文件中提取数据,该文件正在存储媒体文件夹,如名称:“soso” 年龄:“so”

 the code which I tried so far is as below

视图.py:

from django.shortcuts import render
from django.conf import settings
from django.core.files.storage import FileSystemStorage

    def index(request):
        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, 'index.html', {
                'uploaded_file_url': uploaded_file_url
            })
        return render(request, 'index.html')

索引.html:

{% block content %}
  <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 %}
  <p><a href="{% url 'home' %}">Return to home</a></p>
{% endblock %}

标签: pythondjangofile-system-storage

解决方案


推荐阅读