2017-03-02 148 views
2

我想上传一个文件到我的数据库,并在上传后导入它并最终将数据导出到我的数据库中。我有上传工作正常,但我不知道如何获得文件上传后的绝对路径。我可以打印文档的名称,但是如果上传相同的文档名称,它会被追加,但如果我拨打form.cleaned_data['document'].name,仍会显示原始文件名称。我能做些什么来获得绝对文件路径,然后调用一个函数来开始处理这个文件?在Django中上传后如何获取文件绝对路径?

所以这就是我希望做:

  • 用户上传.csv文件
  • 文件被保存在数据库(以及描述和文件路径文件路径是越来越妥善保存在刚上传
  • 开始处理此文件,将.csv数据和存储转换的数据库文件的DB)
  • 获取文件位置

models.py

from django.db import models 

# Create your models here. 
class Document(models.Model): 
    description = models.CharField(max_length=255, blank=True) 
    document = models.FileField(upload_to='documents/') 
    uploaded_at = models.DateTimeField(auto_now_add=True) 

views.py

from django.shortcuts import render, redirect 
from django.views import View 
# Create your views here. 

from .forms import DocumentForm 
from .models import Document 

class FileUpload(View): 
    def post(self, request): 
     form = DocumentForm(request.POST, request.FILES) 
     if form.is_valid(): 
      print() 
      print(form.cleaned_data['document'].name) 
      form.save() 
      return redirect('main_db_model:home') 
     else: 
      return render(request, 'file_upload_form.html', { 
       'form': form 
      }) 

    def get(self, request): 
     form = DocumentForm() 
     return render(request, 'file_upload_form.html', { 
      'form': form 
     }) 

forms.py

from django import forms 
from .models import Document 

class DocumentForm(forms.ModelForm): 
    class Meta: 
     model = Document 
     fields = ('description', 'document',) 

file_upload_form.html(模板):

{% extends "base.html" %} 

{% block content %} 
    <form method="post" enctype="multipart/form-data"> 
    {% csrf_token %} 
    {{ form.as_p }} 
    <button type="submit">Upload</button> 
    </form> 
     {% if saved %} 
     <strong>Your profile was saved.</strong> 
     {% endif %} 
    This is the form page 
    <p><a href="{% url 'main_db_model:home' %}">Return to home</a></p> 

    <p>Uploaded files:</p> 
     <ul> 
     {% for obj in documents %} 
      <li> 
      <a href="{{ obj.document.url }}">{{ obj.document.name }}</a> 
      <small>(Uploaded at: {{ obj.uploaded_at }})</small> 
      {{ obj.document.url }} 
      </li> 
     {% endfor %} 
     </ul> 
{% endblock %} 

回答

2

以前,我建议你改变这个为什么?到upload_to='documents/%Y/%m/%d',为什么?这将处理您的documents/路径中的大量文档文件。


form.cleaned_data['document'](或request.FILES['document'])返回一个UploadedFile对象。当然form.cleaned_data['document'].name应该只返回一个名字。

class FileUpload(View): 
    def post(self, request): 
     form = DocumentForm(request.POST, request.FILES) 
     if form.is_valid(): 
      # this `initial_obj` if you need to update before it uploaded. 
      # such as `initial_obj.user = request.user` if you has fk to `User`, 
      # if not you can only using `obj = form.save()` 
      initial_obj = form.save(commit=False) 
      initial_obj.save() 

      # return path name from `upload_to='documents/'` in your `models.py` + absolute path of file. 
      # eg; `documents/filename.csv` 
      print(initial_obj.document) 

      # return `MEDIA_URL` + `upload_to` + absolute path of file. 
      # eg; `/media/documents/filename.csv` 
      print(initial_obj.document.url) 

      form.save() 

但是,如果你使用upload_to='documents/%Y/%m/%d'你会得到一个不同的,

print(initial_obj.document)  # `documents/2017/02/29/filename.csv` 

print(initial_obj.document.url) # `/media/documents/2017/02/29/filename.csv` 
+0

非常感谢你对此的解释。我会采取这种方法,因为它是最有意义的! – Ducksauce88

相关问题