"UnicodeEncodeError",'charmap' codec can't encode characters In Django - django

I am working with Django.
I have a HTML form which collects information from the user,wrote a function which post the value.But when I amtrying to render the value to an URL.(Example:return of the function is like:http://api.test.com?search=topic?title=USA).
But it shows UnicodeEncodeError with
'charmap' codec can't encode characters
I googled and try to solve from other solution found.Such as using external libraries.
Actually,I want to add user parameter within URL that will be shown in a text area.
How can I do that?

You should use Urllib.urlencode to create get parameters to be added to the url.
I guess, that should solve your problem

Related

How to use filepond with Django

As the title suggest.
I have searched Google and stackoverflow, so far I don't find any tutorial that doesn't involve (https://github.com/ImperialCollegeLondon/django-drf-filepond).
While this library seems maintain, at 68 stars, too much risk and I prefer to do without it.
What I tried
When you use filepond input tag with class file-uploader file-uploader-grid, in browser, it will compile and generate a div tag.
The issue is that the id in input will be generated under the div instead of input tag.
Without that id, when the form is submitted, self.request.FILES will be empty dictionary.
So I tried writing a JavaScript to add id to input tag, which don't work unfortunately.
Anyone successfully do it in Django without additional library? Thanks
The input generated is only there to catch files, the actual data is either stored in hidden input fields (if you use server property) or encoded in those fields (if you use file encode plugin).
You can set storeAsFile to true to have FilePond update the fileList property of a file field. But that doesn't work on older versions of iOS, see link in property description:
https://pqina.nl/filepond/docs/api/instance/properties/

How to fix 'Imported file has a wrong encoding: 'charmap' codec can't decode byte 0x9d in position 21221: character maps to' error?

I'm trying to import a csv file to my django project. Until now, the previous times I did this i never had a problem. However, all of a sudden I keep getting this error that says "Imported file has a wrong encoding: 'charmap' codec can't decode byte 0x9d in position 21221: character maps to" when i try to import the csv file in.
I don't understand why I am getting the error
[![enter image description here][1]][1]
this is what i keep getting.
I am trying to import my excel file like this:
[![enter image description here][2]][2]
and this is what my csv file looks like:
[![enter image description here][3]][3]
\
The csv file contains invalid data for the encoding that you are attempting to interpret it with. Depending on how it was generated, you might be able to tell Python what is the correct decoding to apply when you open it
f = open(csv_file_name, encoding= ...)
or you might specify an appropriate encoding when you generate the csv file, or you might be processing dodgy data and have to resort to encoding="latin-1" -- which may result in putting bad data in to your database, if you don't validate what comes out of the csv file through a Django form before saving it.
I'd recommend always processing rows of csv data through Django forms or modelforms. It makes it very easy to catch errors (form is not valid, form.errors, etc. ) and print out useful error messages about what is wrong with which field (column) of the row.
Lots more here and here
Hex character 9d is not a printable character (https://www.codetable.net/hex/9d). In unicode it appears to be some kind of command. You will need to sanitise this character to be able to handle it in a csv file.
Edit: As #snakecharmerb points out in the comments, there are encodings where this is a valid character. However, I suspect from your question that you are not using one of these.
You can also look into decode to allow you to specify a charset for reading the data. If you have a charset that you think this is a valid char for, then perhaps your routine is picking up a different default charset from the system.
I actually do something like this to make sure I get Swedish chars properly set. This is directly from my code when extracting fields
output.decode('iso-8859-1').strip()

Getting WebPage to use a specific URL to download HTML resources

I have a Qt program that downloads webpages (HTML), parses them and then generates its own HTML which is then displayed with QWebPage. Some times the HTML that I download contains IMG tags, which work fine when the src attribute contains a full URL. However, some times the IMG tag might use a relative path like:
<IMG SRC="images/foo.png" />
Since I know the URL that should be prepended to the SRC my first thought was to just tack it onto my resulting HTML when I'm parsing. However, this is proving more difficult than I anticipated and now I'm wondering if there's a better way.
If there any mechanism/property with QWebPage that I can say "use this URL for relative paths"? Or maybe someone can suggest a better way to accomplish what I want?
Thanks!
In the comments, you mentioned that you're using QWebView::setHtml(). The second, optional parameter of this method sets the URL to use for resolving relative paths. According to the documentation:
External objects such as stylesheets or images referenced in the HTML
document are located relative to baseUrl.
Setting that parameter should be all that's needed here.

What can't Django decode my template?

I saved a Microsoft Word document as an HTML document. I then copied and pasted the generated HTML into a Django template "document.html".
I then mapped a URL to a simple view that loads this template.
def viewDocument(request):
return render_to_response('document.html')
Afterwards, I get this error when access the page for that view.
UnicodeDecodeError at /viewDocument/
'utf8' codec can't decode byte 0xd2 in position 67600: invalid continuation byte
Strangely, the HTML in document.html displays fine when I open the page generated from Microsoft Word in Google Chrome on my laptop. How come it doesn't work as a Django template?
Because the file isn't encoded in UTF-8.

Plot a graph in the html file using Django

I am doing a monitoring system using Django. In my views file, I have defined one class called showImage which collects the information necessary to plot a graph using matplotlib.
At the beginning, I just stored the image in a string buffer to represent it with HttpResponse:
buffer = StringIO.StringIO()
canvas = pylab.get_current_fig_manager().canvas
canvas.draw()
pilImage = PIL.Image.fromstring("RGB", canvas.get_width_height(), canvas.tostring_rgb())
pilImage.save(buffer, "PNG")
# Send buffer in a http response the the browser with the mime type image/png set
return HttpResponse(buffer.getvalue(), mimetype="image/png")
However, I need to implement some javaScript in the html file to add more applications. For that reason, I have decided to save the image in a variable and plot it in the html file:
# serialize to HTTP response
response = HttpResponse(buffer.getvalue(), mimetype="image/png")
return render_to_response('eQL/dev/showImage.html', {'response':response})
My question is that I don't really know how to represent it in the html file because I didn't find any example doing it. Any one knows the answer?
Thanks in advance!
Do you mean that in your first implementation, your response was a PNG file, but now you wish to make the response an HTML file instead, containing the image?
Well firstly, you need to change the response MIME type from image/png to text/html or similar.
Secondly, I'm not sure why you are passing a HttpResponse object (containing the PNG data) into the template. Can the template even read that? Surely you just want to be passing the raw PNG data, not a HttpResponse object.
Finally, how to do it. Well as you may know, HTML isn't so great at embedding images. As with normal websites, you can include text in the page, but if you want an image, you need a separate file and link to it using the <img src="..." /> element. This is tricky to do dynamically: it means you need to setup two separate URLs (one for the PNG and one for the HTML), which run independently of one another (you can't just have one piece of code; you need one handler for generating the PNG and the other for generating the HTML), and have the HTML link to the PNG URL.
If that is too hard, there is another way out, but it is a bit hacky: data URLs. They let you include image data in the HTML page itself, so you only need to produce one response. Unfortunately it is not well supported in Internet Explorer pre-9. IE8 supports images less than 32K, IE7 and below don't work. See the example on Wikipedia -- you are aiming to generate something like this:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />
Basically, take the PNG data, and Base64-encode it (use Python's base64 library). Then just put "data:image/png;base64," in front of it, and set that as the URL for the img src. In other words, pass the Base64-encoded string to Django's template engine, and construct the URL as part of the img tag in the template.