Finally I've solved it with smart_str function.


Matias Surdi wrote:
> Hi, I'm trying to save on a FileField some generated data (a text file 
> obtained from a template):
> 
> The relevant code is:
> 
>              out = Template(open("/path/to/template").read())
>              context = Context({})
>              if request.POST["name"] != "":
>                  name = request.POST["name"]
>              else:
>                  name = "GeneratedDocument_%i" % obj.id
>              data = out.render(context)
>              obj.file.save("file",ContentFile(data))
> 
> 
> the problem arises with ContentFile(data), if data is just a string like 
> "a simple string" the everything is correct, but the actual data content 
> is an unicode string, so raw binary data is saved to the resulting file 
> instead of the actual text.
> 
> Simplifying, what I want is to save the output of a rendered template to 
> a FileField.
> 
> Any help will be very appreciated.
> 
> 
> Thanks a lot.
> 
> 
> > 
> 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to