Html 如何显示最小文件上传django示例中需要的上传文件大小
我需要在html代码中显示文件大小以及视图和模型中的更改。请帮助我做到这一点 views.py 这是我的视图代码。上载时显示文件大小的更改Html 如何显示最小文件上传django示例中需要的上传文件大小,html,django,Html,Django,我需要在html代码中显示文件大小以及视图和模型中的更改。请帮助我做到这一点 views.py 这是我的视图代码。上载时显示文件大小的更改 def index(request): response = TemplateResponse(request, 'login.html', {}) return response def login(request, username, password): user = username[:-1] for i
def index(request):
response = TemplateResponse(request, 'login.html', {})
return response
def login(request, username, password):
user = username[:-1]
for i in AppUser.objects.all():
if ( user == i.username and password == i.password ):
return list(request)
else:
return index(request)
def list(request):
# Handle file upload
print "im getting in"
if request.method == 'POST':
form = DocumentForm(request.POST, request.FILES)
if form.is_valid():
newdoc = Document(docfile = request.FILES['docfile'],created_at = datetime.datetime.now())
newdoc.save()
# Redirect to the document list after POST
return HttpResponseRedirect(reverse('cloudStorageManager.views.list'))
else:
print "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, 'created_at': datetime.datetime.now()},context_instance=RequestContext(request))
models.py
用于显示文件大小的属性
class Document(models.Model):
docfile = models.FileField(upload_to='documents')
created_at=models.DateTimeField(auto_now_add=True , blank=True)
list.html
上载时显示文件大小的html代码
{% for document in documents %}
<tr>
<td>
<input type="checkbox" class="case" id="chk{{forloop.counter}}" value="{{ document.docfile.name }}">
</td>
<td>{{ document.docfile.name }} {{ document.created_at }}</td>
<td><a href="{{ document.docfile.url }}" target="_blank" title="Download"><button class="btn btn-success btn-flat"><span class="glyphicon glyphicon-cloud-download"> </span> Download</button></a></td>
<td>
{%用于文档中的文档%}
{{document.docfile.name}{{document.created_at}}
文件大小。用于filesizeformat筛选器。您可以知道forms.py文件中上载的文件的大小并进行验证
class DOcumentForm(...):
...
docfile = forms.FileField(...)
...
def clean_docfile(self):
current_size = self.cleaned_data['docfile'].size
if current_size < 2621440 # size in bytes:
return self.cleaned_data['docfile']
else:
raise forms.ValidationError('Max 2.5 MB allowed. Current size is {0}'.format(current_size))
类文档表单(…):
...
docfile=forms.FileField(…)
...
def clean_docfile(自):
当前大小=自清理的数据['docfile'].size
如果当前_大小<2621440#大小(字节):
返回自清理的_数据['docfile']
其他:
raise forms.ValidationError('允许的最大值为2.5 MB。当前大小为{0}'。格式(当前大小))
以上代码是允许的最大文件大小。如果您想要最低要求,只需颠倒
class DOcumentForm(...):
...
docfile = forms.FileField(...)
...
def clean_docfile(self):
current_size = self.cleaned_data['docfile'].size
if current_size < 2621440 # size in bytes:
return self.cleaned_data['docfile']
else:
raise forms.ValidationError('Max 2.5 MB allowed. Current size is {0}'.format(current_size))