我想在用户上传文件时创建一个应用程序,然后将其输入系统,并执行linux命令,如" xxd"。
cSite---cSite---settings.py
| |__etc...
|
--myapp---
这是我的文件上传代码。
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')
如您所知,此代码是广泛的简单文件上传表单。
现在,这是我的麻烦。
保存文件时想要使用salt (我可以使用" Keeping Original Filename for FileField in Django"?如果可以的话,如何申请?
我不知道./views.py/上FileField中的文件路径变量代表什么。它总是包含错误。 (该命令必须在用户视图中上传文件的同一时间后执行。)