我想设置文件系统存储位置中提供的文件的正确URL
views.py
if request.method=='POST' and request.FILES.get('myfile', None):
myfile=request.FILES['myfile']
print(myfile.name.split('.'))
image_name_banner = image_name +'-' + 'banner' + '.' +myfile.name.split('.')[-1]
print(image_name_banner)
myfile.name = image_name_banner
fs=FileSystemStorage(location='media/events/images')
print(fs)
filename=fs.save(myfile.name,myfile)
print(filename)
print(fs.url)
uploadedfileurl=fs.path(filename)
u_banner=uploadedfileurl
print(u_banner)
u_banner将默认URL打印为media / filename.jpg
答案 0 :(得分:0)
def upload_example(request):
if request.method == 'POST' and request.FILES['myfile']:
myfile = request.FILES['myfile']
fs = FileSystemStorage(location='media/events/images/')
filename = fs.save(myfile.name, myfile)
uploaded_file_url = fs.url(filename) # file url
return render(request, 'yourpagehere.html', {
'uploaded_file_url': uploaded_file_url
})
return render(request, 'yourpagehere.html')
uploaded_file_url
包含上载文件的网址
有关文件上传的更多信息,可以在docs
中找到