- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个 django 管理页面,用户正在上传文件,数据在内存中读取,而不实际存储文件。
根据内容,在磁盘上生成一个新文件。这一切都发生在调用 save_model
之前的 clean
方法中。下面是生成的方法输出文件
def clean(self):
obj = Parser()
cleaned_data = super(MyMethodAdminForm, self).clean()
input_data = self.request.FILES['file_name'].read().split('\n')
base_file = settings.MEDIA_ROOT + "/uploads/" + cleaned_data.get('file_name').name
# This is the output file, which needs to be downloaded
output_filename = base_file.replace('.csv', '_output.csv')
output_list = []
# Read the file and prepare the output data
# on the basis of the input data
for line in input_data:
data = line.split(",")
if len(data) > 1:
dt = obj.get_data()
current_list = [data[0], data[1]]
else:
dt = obj.get_data()
current_list = [data[0]]
current_list.append(str(dt))
output_list.append(current_list)
# Prepare the file on the basis of the output data
with open(output_filename, 'a') as outcsv:
# configure writer to write standard csv file
writer = csv.writer(outcsv, delimiter=',', quotechar=' ', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
for item in output_list:
if len(item) > 2:
# Write item to outcsv
writer.writerow([item[0], item[1], item[2]])
else:
writer.writerow([item[0], item[1]])
return cleaned_data
现在,我的问题是我必须允许此文件下载,但由于上传域是通过负载均衡器处理的,所以我无法真正提供链接(老实说,也不是首选方式)。有什么方法可以强制提示文件下载?
我从下面两个问题中得到了一些提示,但后来不明白我需要将这些更改标题内容放在哪里
编辑
经过更多调试后,我尝试按照线程 Redirect on admin Save 中的建议更新 response_change
class MyMethodAdmin(admin.ModelAdmin):
form = MyMethodAdminForm
def response_change(self, request, obj):
import ipdb;
ipdb.set_trace()
# response = HttpResponse(content_type='text/csv')
# response['Content-Disposition'] = 'attachment; filename="%s"' % form.cleaned_data['output_file_path'] (somehow access the file-path, its incorrect ryt now, i know
super(MyMethodAdmin, self).response_change(request, obj)
但即使 pdb 也没有被调用......所以还没有帮助! :(
完成类(class):
class MyMethodAdminForm(forms.ModelForm):
class Meta(object):
model = MyMethod
fields = ["file_name"]
def clean(self):
obj = Parser()
cleaned_data = super(MyMethodAdminForm, self).clean()
input_data = self.request.FILES['file_name'].read().split('\n')
base_file = settings.MEDIA_ROOT + "/uploads/" + cleaned_data.get('file_name').name
output_filename = base_file.replace('.csv', '_output.csv')
output_list = []
# Read the file and prepare the output data
# on the basis of the input data
for line in input_data:
data = line.split(",")
if len(data) > 1:
dt = obj.get_data()
current_list = [data[0], data[1]]
else:
dt = obj.get_data()
current_list = [data[0]]
current_list.append(str(dt))
output_list.append(current_list)
# Prepare the file on the basis of the output data
with open(output_filename, 'a') as outcsv:
# configure writer to write standard csv file
writer = csv.writer(outcsv, delimiter=',', quotechar=' ', quoting=csv.QUOTE_MINIMAL, lineterminator='\n')
for item in output_list:
if len(item) > 2:
# Write item to outcsv
writer.writerow([item[0], item[1], item[2]])
else:
writer.writerow([item[0], item[1]])
return cleaned_data
最佳答案
无论您准备在何处向用户发送响应:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
writer = csv.writer(response)
# now write to writer however you want to output your CSV
<小时/>
您可能需要重写多种方法,具体取决于您调用 View 的位置。原因是 django 支持不同类型的管理操作,包括 change
、add
、delete
which you can read about here 。我不确定您使用的是哪一种,因此您可能必须同时尝试两种。第一步,我将尝试覆盖 response_change
和 response_add
方法:
class MyMethodAdmin(admin.ModelAdmin):
form = MyMethodAdminForm
def return_csv(self):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
writer = csv.writer(response)
writer.writerow(['Django','Admin'])
return response
def response_change(self, request, obj):
return self.return_csv()
def response_add(self, request, obj):
return self.return_csv()
关于python - django admin 上传后提示文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41215661/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!