Python Django:使用文件字段
我想在用户上传文件时制作一个应用程序,所以将它放到系统中,并执行像“xxd”这样的linux命令 这是我的文件上传代码 views.py: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
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
上的url是什么?它是一个上载的“文件路径”。我找不到该文件的字符串类型。你能解释第二个问题吗?views.py
上的url是什么?它是一个上载的“文件路径”。我找不到该文件的字符串类型。
from django.db import models
class Document(models.Model):
docfile = models.FileField(upload_to='documents/%Y/%m/%d')