Python Django:使用文件字段

Python Django:使用文件字段,python,django,web,Python,Django,Web,我想在用户上传文件时制作一个应用程序,所以将它放到系统中,并执行像“xxd”这样的linux命令 这是我的文件上传代码 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

我想在用户上传文件时制作一个应用程序,所以将它放到系统中,并执行像“xxd”这样的linux命令

这是我的文件上传代码

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')
正如你所知,这段代码是广泛传播的简单文件上传形式

现在,我的麻烦来了

  • 我想在保存文件时使用盐吗 (我可以使用“”吗?如果可以,如何应用

  • 我不知道是什么代表了./views.py/上FileField中的file path变量,它总是包含错误。(该命令必须在用户视图中上载文件的同时执行。)


  • 你能解释第二个问题吗?
    views.py
    上的url是什么?它是一个上载的“文件路径”。我找不到该文件的字符串类型。你能解释第二个问题吗?
    views.py
    上的url是什么?它是一个上载的“文件路径”。我找不到该文件的字符串类型。
    from django.db import models
    
    class Document(models.Model):
        docfile = models.FileField(upload_to='documents/%Y/%m/%d')