python - Django - permission denied for text file created on the fly -
i trying make downloadable text file on fly, think have achieved when run code permission denied error.
also when open text file, created anywhere in file system? dont want store these files, create them , have them downloaded users machine
ioerror @ /networks/configs/str-rtr-01/7 [errno 13] permission denied: u'str-card-rtr-01.txt'
config:
def configs(request, device, site_id): site = get_object_or_404(showroomconfigdata, pk=site_id) config_template = get_object_or_404(configtemplates, device_name=device) file_name = device[:4] + site.location.upper()[:4] + "-" + device[4:] + ".txt" device_config = none open(file_name, 'w') config_file: device_config = env.from_string(config_template.config) device_config.stream( str = site.location.upper()[:4], ip = site.subnet, bgpasno = site.bgp_as, loip = site.r1_loopback_ip, location = site.location, date = site.opening_date, ).dump(config_file) return render(request, file_name, { })
if goal provide link user can download file automatically generated, there's no need write disk.
you can build desired content in python string, , use content-disposition
header suggest user's browser should download file rather display it, , filename
parameter specify default filename user save file as.
a simpler example of view function this...
from django.http import httpresponse def get_config_file(request): filename = 'config.txt' content = 'this content of config file' content_type = 'text/plain' content_disposition = 'attachment; filename=%r' % filename response = httpresponse(content, content_type=content_type) response['content-disposition'] = content_disposition return response
Comments
Post a Comment