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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -