2017-03-02 151 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

相關問題