UnicodeDecodeError in django 1.6 and allauth - django

I'm trying to run a django website on my local computer. It works fine on an external server, but I didn't set it up and right now I don't have access to all the specs.
The issue I have is when I try to log in the web site as a user, which has been defined. Running in debug mode I get a detailed error page containing on top the message:
UnicodeDecodeError at /accounts/login/
'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)
Looking down I can see that the error occurs in crypto.py, function salted_hmac at the line
key = hashlib.sha1((key_salt + secret).encode('utf-8')).digest()
and displaying the local variables I see
key_salt u'django.contrib.sessionsSessionStore'
secret '\xe2\x80\x9cXXX"'
value '{}'
where XXX is a 50 character string identical to the SECRET_KEY defined in my configuration file. Variable secret is assigned in the function through:
if secret is None:
secret = settings.SECRET_KEY
and I know that secret is None at this point since it is a third parameter in salted_hmac not used by the caller. I strongly suspect that the error occurs because python can not handle the unicode characters at the beginning of the variable secret.
So I have a few questions:
1) Why is setting.SECRET_KEY different from the SECRET_KEY I defined in the configuration file? Is it how it should be? And if it is do I have any control over what it should be?
2) Could something in my environment be responsible for this?
A few notes: As I mentioned it works on a server, running ubuntu 1.6, python 2.7. However I can not right now obtain the info on the versions for the other packages. But even if I could I still want to know why it doesn't work on my installation. I have tested with django 1.6.1, python 2.7 on lubuntu 14.04, opensuse 13.2, with identical results.
Thanks for any help or hint.
a

Problem solved: the secret key in my configuration file started with an (unicode) opening quote, instead of a regular double quote, probably a result of copying and pasting the original file that had been written with an editor that uses these "smarter" quotes.

Related

Django utf-8 urls

I have a Django app that works fine on localhost.even for utf-8 URL path.but when I use it in production it gives me an error:
2019-09-01 14:32:09.558237 [ERROR] [12257] wsgiAppHandler pApp->start_response() return NULL.
Traceback (most recent call last):
File "/home/medualla/virtualenv/project/3.7/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 139, in call
set_script_prefix(get_script_name(environ))
File "/home/medualla/virtualenv/project/3.7/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 179, in get_script_name
script_url = get_bytes_from_wsgi(environ, 'SCRIPT_URL', '') or get_bytes_from_wsgi(environ, 'REDIRECT_URL', '')
File "/home/medualla/virtualenv/project/3.7/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 204, in get_bytes_from_wsgi
return value.encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 1-6: ordinal not in range(256)
this error occurs when i try a url like
http://meduallameh.ir/صفحه
the only answer I got was that problem with the webserver. I deployed it on a shared host and I asked them and they told me that web server supports utf-8. now I need some help to fix this problem.
After dealing with some code and searching for the problem I figured out that problem was that SCRIPT_URL and other stuff are decoded to utf-8 by default in the host. so it gives an error for that. I fixed it temporarily with changing get_bytes_from_wsgi return statement to this;
def get_bytes_from_wsgi(environ, key, default):
"""
Get a value from the WSGI environ dictionary as bytes.
key and default should be strings.
"""
value = environ.get(key, default)
# Non-ASCII values in the WSGI environ are arbitrarily decoded with
# ISO-8859-1. This is wrong for Django websites where UTF-8 is the default.
# Re-encode to recover the original bytestring.
return value.encode('utf-8')
so the problem solved(for now). I figured out that this happens for many headers and especially with files. if someone finds another way that can fix, please write here
At one point, I had UTF-8 characters in the URL working for me, and somehow, I noticed that it broke when I migrated from Python 2 to Python 3 (along the way, I also switched from Apache + mod_wsgi to Apache proxying to Gunicorn).
Based on the answer the OP provided, I reluctantly resorted to subclassing the default WSGIHandler and WSGIRequest.
Gist: https://gist.github.com/jontsai/afd5f5d9399ac2b0d770a73983d61690#file-django_wsgi_utf8_handler-py-L66-L69
While not ideal, this solution can be deployed to multiple servers/environments.
I'll be looking to try to file a patch with the Django project.

Can't convert string from 'UTF-8' to native encoding for pysvn export

I was running pysvn's export of a repo with file names with french letters like "Français" or "Spécifications du produit". When the export runs, the following error is returned
Can't convert string from 'UTF-8' to native encoding
I found this http://refactor.se/2007/08/13/svn-fix-cant-convert-string-from-utf-8-to-native-encoding/ and Can't convert string from 'UTF-8' to native encoding
indicating something about setting the local language but I couldn't find this in pysvn. Is it possible to set this when initializing a pysvn Client?
EDIT: Forgot to mention i'm doing this on django 1.7
Thanks
In case somebody comes across this in the future, what i did was i copied the initLocale() function from pysvn in this file
http://pysvn.tigris.org/source/browse/pysvn/trunk/pysvn/Extension/Examples/Client/svn_cmd.py?view=log
and then called this before the initialization of the svn Client. SVN worked afterwards

I'm getting "'utf8' codec can't decode byte #x96: invalid start byte" error in GAE Launcher

I am getting an error message when trying to run my Python API via the Google App Engine Launcher. I'm following a set of instructions by "Ashutosh KS" to launch a website on the Google Server that I found at http://www.hongkiat.com/blog/host-website-google-server/.
I get the error message below when I run the App.yaml file.
google.appengine.api.yaml_errors.EventListenerYAMLError: 'utf8' codec can't decode byte #x96: invalid start byte
in "C:\healygeoscience\app.yaml", position 99
2015-02-17 01:48:53 (Process exited with code 1)
When I attempt to deploy the API, I also get the following error.
appcfg.py: error: Error parsing C:\healygeoscience\app.yaml: 'utf8' codec can't decode byte #x96: invalid start byte
in "C:\healygeoscience\app.yaml", position 99.
2015-02-17 01:50:04 (Process exited with code 2)
My App.yaml file is as follows:
application: healygeoscience
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
– url: /
static_files: www/index.html
upload: www/index.html
– url: /
static_dir: www
libraries:
– name: webapp2
verssion: "2.5.2"
The website is a single page website based on the Ceevee template from http://www.styleshout.com/demo/?theme=ceevee., which I edited with TextPad. The website is operational when run locally, although I have to refresh several times to get all images to appear - possibly significant ?
From reading other questions on Stackoverflow, I realise that the error is due to an issue of encoding, and am wondering if the statement on line 8 in the header section of the index.html file is significant. Some posts suggest decode from charset=ISO-8859-1 to unicode. Can you explain and offer code? From TextPad, I have saved index.html file in UTF-8 and Unicode, and from WordPad I saved App.yaml to Unicode Text Document , but error persists!
Does position 99 refer to line numbers, and if so, does this include the fourteen lines of code in App.yaml, and also include blank lines in App.yaml and index.html? How do I locate the string with #x96.
I suspect a lot of novices trying to set up personal and business websites will encounter this issue, so it would probably be very beneficial if someone could offer some advice to a complete Python novice.
RaymondHealy
If you copied the contents of the app.yaml from the hongkiat walkthrough before editing, you need to retype the '-' characters.
There are many subtly different characters that resemble '-' and are automatically 'corrected' by many text editors, in this case those in the walkthrough are not recognised.
99 refers to the character number. You can find which character is referred to by reading the file into a Python interpreter and selecting 99:
file_contents = open('app.yaml').read()
print(file_contents[99])

Django Mezzanine Unicode Error

i get an unicode error when trying url like www.mysite.com/blog/category/πρακτικα/ or www.mysite.com/blog/πρακτικα/
but i dont get the error when trying www.mysite.com/blog/tag/πρακτικα/
UnicodeEncodeError at /blog/category/πρακτικα/ 'latin-1' codec can't encode characters in >position 58-65: ordinal not in range(256)
Exception Location: /home/vagrant/sullogos-venv/local/lib/python2.7/site-packages/django/template/loaders/filesystem.py in load_template_source, line 37
seems it haves different behavior at categories and at tags
The difference is that categories can have a custom template and tags can't. So in the category case, a template name is searched for using the category slug - the error you're getting is due to an incorrectly configured locale which doesn't support utf8.
This is not a problem with Mezzanine or Django, but with the environment used to deploy them. See this issue and this documentation for more details. It's not enough for Python to support a specific locale, but it's also necessary for the webserver to be able to handle Unicode files correctly.
How to fix it will depend on the webserver used. If you're using Apache, for instance, you need to set LANG and LC_ALL to Unicode-compatible values (on *NIX systems at least you should find them at /etc/apache2/envvars). An example would be:
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
Feel free to replace the language/country codes with another one more suitable for your needs (I used pt_BR instead of en_US and things worked fine for me). From the error message you're seeing, these settings in your system are probably using ISO-Latin (ISO-8859-1) instead of UTF-8 (which I assume can't handle cyrillic).
If you're using a different webserver, check its documentation on localization/internationalization to see what needs to be changed. The important thing is to offer support to Unicode file names, as I understood.

Unicode in django admin

I have problem when i trying add pictures with not only ascii characters through admin interface.
It always seems to trying convert name of picture's file to ascii:
UnicodeEncodeError at /admin/app/subpicture/add/
('ascii', u'/home/celtrun/rails/alphasteel/public/media/my_media/subpictures/Zdj\u0119cia_iPhone_wrzesie\u0144_320.jpg', 68, 69, 'ordinal not in range(128)')
Request Method: POST
Django Version: 1.3
Exception Type: UnicodeEncodeError
Exception Value:
('ascii', u'/home/celtrun/rails/alphasteel/public/media/my_media/subpictures/Zdj\u0119cia_iPhone_wrzesie\u0144_320.jpg', 68, 69, 'ordinal not in range(128)')
Exception Location: /bin/python-2.6.1/lib/python2.6/genericpath.py in exists, line 18
Python Executable: /bin/python-2.6.1/bin/python
And:
Unicode error hint
The string that could not be encoded/decoded was: s/Zdjęcia_i
Result is that the picture that i try to save don't appear in specified repository. The picture's files are saved in this repository if its names don't have unicode characters in file's name.
Has someone have any ideas to fix this?
After 2 years I'm having same problem with CentOS6.4 and Django 1.5.
While I search a solution I think that even I upload a file with special characters I may encounter problems with other applications. So I tried this. Using ASCII for files is better than using unicode names. Not every program support ığüşöçâİ or font. It may also cause font problems in websites.
The default Apache environment on Ubuntu/*nix doesn't allow non-ASCII file names. This leads to
the rather unintuitive UnicodeEncodeError when submitting unicode filenames in a filefield/imagefield
in Django admin.
Try adding this to the Apache config (/etc/apache2/envvars):
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
And then restart the apache server.