[英]Django user specific folders
I want to create user specific folders for files uploaded by users. 我想为用户上传的文件创建用户特定的文件夹。 This is my
views.py
: 这是我的
views.py
:
@login_required
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('upload.views.list'))
else:
form = DocumentForm() # An 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(
'upload/list.html',
{'documents': documents, 'form': form},
context_instance=RequestContext(request)
)
This is my models.py
: 这是我的
models.py
:
class Document(models.Model):
docfile = models.FileField(upload_to='uploads/%Y.%m.%d')
My idea is this. 我的想法是这样。 Instead of naming it
uploads/%Y.%m.%d
I stick the username somewhere in there. 我没有将其命名为
uploads/%Y.%m.%d
而是将用户名放在其中。 Is there a way to do that? 有没有办法做到这一点?
I do something like that in my models.py
: 我在我的
models.py
做了类似的事情:
def _upload_path(instance,filename):
return instance.get_upload_path(filename)
class Document(models.Model):
docfile = models.FileField(upload_to=_upload_path)
user = models.ForeignKey('auth.User')
def get_upload_path(self,filename):
return "static/uploads/"+str(self.user.id)+"/"+filename
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.