mardi 24 novembre 2015

Django: Using the FileFeild

I want to make a application when the user upload the file, so get it to system, and execute the linux command like "xxd".

cSite---cSite---settings.py
      |       |__etc...
      |
      --myapp---

Here is my file upload codes.

views.py :

from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from myapp.models import Document
from myapp.forms import DocumentForm

def list(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

        # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('myapp.views.list'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    documents = Document.objects.all()

    # Render list page with the documents and the form
    return render_to_response(
        'list.html',
        {'documents': documents, 'form': form},
        context_instance=RequestContext(request)
    )

models.py :

from django.db import models

class Document(models.Model):
    docfile = models.FileField(upload_to='documents/%Y/%m/%d')

As you know, this code is widespreaded simple file upload form.

Now, here is my trouble.

  1. Want to use the salt when I save the file (Can I use the "Keeping Original FIlename for FileField in Django"? and if I can, how to apply this?

  2. FileFeild can't use like "url" on ./views.py/ It always contain the error. (The command must be executed at the right after same time of file uploaded in user view.)




Aucun commentaire:

Enregistrer un commentaire