python - Getting Uploaded filename Extension in Django View? -
im working through alex pale's minimal django file upload example django 1.8 -
i know how file extension in form, how can in view. i'm aware can access file thru -
docfile=request.files['docfile']
view -
from django.shortcuts import render_to_response django.template import requestcontext django.http import httpresponseredirect django.core.urlresolvers import reverse myproject.myapp.models import document myproject.myapp.forms import documentform def list(request): # handle file upload if request.method == 'post': form = documentform(request.post, request.files) if form.is_valid(): docfile=request.files['docfile'] # redirect document list after post return httpresponseredirect(reverse('myproject.myapp.views.list')) else: form = documentform() # empty, unbound form # load documents list page documents = document.objects.all() # render list page documents , form return render_to_response( 'list.html', {'documents': documents, 'form': form}, context_instance=requestcontext(request) )
form -
from django import forms class documentform(forms.form): docfile = forms.filefield( label='select file' )
if asking extension try this:
>>> f = "this.that.ext" >>> e = f.split(".")[-1] >>> f 'this.that.ext' >>> e 'ext' >>>
you know how complete filename. want last string after "."
Comments
Post a Comment