Django makemessages writes fake files - python

I'm getting an odd error when I make the i18n files of a django project:
(venv) user#machine:~/path/to/repo$ django-admin makemessages -l es
It creates fake .py files for every .txt files:
For example, requirements/base.txt
Django==1.10.6
django-environ==0.4.1
djangorestframework==3.6
psycopg2==2.7
djangorestframework-jwt==1.9.0
Markdown==2.6.8
unipath==1.1
It generates a requirements.base.txt.py with 'XXXXXX' in it:
XXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXX
XXXXXXXXXXXX
But it also creates the right .po files into /locale
Could you please point me in the right direction? Because I'm lost. Thank you!

For everyone that is having the same problem, this can also occur if you haven't created a locale/ folder within each app of your Django project, as it's the default folder if none specified in your settings.
After creating it, my problem went away.
More info in the documentation

The answer is here : Permission denied in Django makemessages
To make a long story short: makemessages misbehaves because it doesn't know your project's settings. Actually this should be reported as a defect (if it hasn't already) since most other commands that needs the settings to properly work detect the fact and do raise an ImproperlyConfigured error with an explicit message.

Related

Django makemessages decides to comment already existing translations

When I run manage.py makemessages I found some messages which were in the .po file like this:
msgid "Example"
msgstr "Example"
Transformed to this, after I ran the command:
#~ msgid "Example"
#~ msgstr "Example"
What does #~ means? Since the translation of those messages doesn't work anymore, I suppose it is a comment.
What can I do to prevent Django commenting out (or "#~ing") pre-existing messages in the translation file?
Django will comment-out all messages that are no longer in your code. It won't remove them, so you won't lose it, but that way this messages won't end up in compiled .mo file.
I was facing a similar problem with 3rd party apps. makemessages did not include them in the .po file and when adding them manually makemessages was commenting them out next time.
In my case I had the virtual env symlinked into the project folder. To make makemessages see those 3rd party apps I had to add -s
./manage.py makemessages -a -s -l de -i faker -i openid -i gunicorn
At the same time I want to exclude some apps from translations with -i

Deploying Django to AWS - WSGIPath refers to a file that does not exist

I've been struggling with getting Django and AWS to work together. I'm following the tutorial here:
https://realpython.com/blog/python/deploying-a-django-app-to-aws-elastic-beanstalk/
I've been following all the tutorial steps, including using the "eb option" command to change the WSGIPath, but I keep getting the error:
"ERROR: Your WSGIPath refers to a file that does not exist."
As far as I can tell I've been doing everything exactly according to the tutorial.
The relevant part of my config file looks like this:
NumProcesses: '1'
NumThreads: '15'
StaticFiles: /static/=static/
WSGIPath: iotd/iotd/wsgi.py
What am I doing wrong?
One thing I found when I encountered this error, is that if your repository is a git repository your .ebextensions folder must be tracked and committed otherwise it will not be picked up properly on eb deploy.
I have read the realpython blog post that you referred to. I would also refer you to the AWS tutorial. It is written for the deployment of a bare bones Django project and it can be found at:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-django.html#python-django-configure-for-eb
I found it useful to work through, and learned a great deal fixing the error you have identified. Of course the fix is related to my own implementation of the tutorial, which I followed step-by-step. I have read other posts that talk to this issue, but the solution stated here was not provided in those posts, as far as I can tell.
An abbreviated version of the tutorial follows to provide some context for the comments made here. This abbreviated version begins after creating/activating the virtual environment, but before its activation.
$ mkdir ed_django_app
$ . venv/Scripts/activate
(venv)[~eb_django_app]$ django-admin startproject django_eb
(venv)[~eb_django_app/django_eb]$ python manage.py migrate
(venv)[~eb_django_app/django_eb]$ python manage.py runserver
(venv)[~eb_django_app]$ pip freeze > requirements.txt
(venv)[~eb_django_app]$ deactivate
[~eb_django_app]$ eb init –region us-east-1
After the "eb init" command the .elasticbeanstalk directory, along with some files, are created in the initialization process. In that directory you will find the config.yml file. Its contents are:
branch-defaults:
default:
environment: eb-django-dev
global:
application_name: eb_django_app
default_ec2_keyname: myec2keyname
default_platform: Python 2.7
default_region: us-east-1
profile: eb-cli
sc: null
The tutorial directs the developer to then create a directory called .ebextensions and create the 01-eb_django.config file:
option_settings:
"aws:elasticbeanstalk:application:environment":
DJANGO_SETTINGS_MODULE: "django_eb.settings"
PYTHONPATH: "/opt/python/current/app/django_eb:$PYTHONPATH"
"aws:elasticbeanstalk:container:python":
WSGIPath: "django_eb/django_eb/wsgi.py"
This is YAML and the indentation matters. At least 1 space indent. In this case there are 2 spaces of indent at each level. The WSGIPath is set correctly. It is important to make sure the directory structure is the same as what is indicated in the tutorial.
In the tutorial the "eb create" command is now issued, and as you noted, the following arises:
ERROR: WSGIPath refers to a file that does not exist
The problem that was identified existed in the config.yml where there is the key-pair for application_name:
global:
application_name: eb_django_app
It was changed to:
global:
application_name: django_eb
This resolved the ERROR for me.
Using eb :
eb config
Go to aws:elasticbeanstalk:container:python: and change WSGIPath from:
application.py
to
mysite/wsgi.py
With "mysite" being your application name ofcourse
possible solution error : Your WSGIPath refers to a file that does not exist
after following this tutorial:
https://realpython.com/deploying-a-django-app-to-aws-elastic-beanstalk/
I got the error when I was uploading my protect to aws.
The step that I forgot was to activate my virtual env and from my there type the command 'eb deploy'
note : this error can also occur in different circumstances
This in django.config file, say your app name is 'yourappname',
worked for me.
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: yourappname.wsgi:application

wsgi.py to replace source variables

How would I do the following in my wsgi file, which is imported by apache on production?
$ source env_template.sh
$ python manage.py runserver
>>> Stage: debugging
Validating models...
In my settings file I have stuff like the following, which source sets:
AWS_ACCESS_KEY = os.environ['AWS_ACCESS_KEY']
I would like apache to load all the variables in env_template.sh on starting it. Am I able to do that in the wsgi.py file?
Not in any sane manner. I recommend that you either restrict the file to simple "key=value" lines and then parse it out, or move the specific values to separate files and then read them out in both scripts.

DJANGO_SETTINGS_MODULE is defined, project settings do import via `python manage.py `, yet django-pytest does not find the django settings

Synopsis of problem:
I want to use django-pytest to test my django scripts yet py.test complains that it cannot find DJANGO_MODULE_SETTINGS. I have followed the documentation of django-pytest for setting DJANGO_MODULE_SETTINGS. I am not getting the expected behavior from py.test.
I would like help troubleshooting this problem. Thank you in advance.
References—django-pytest documentation
Setting the DJANGO_SETTINGS_MODULE in virtualevn postactivate script:
In a new shell, the $DJANGO_SETTINGS_MODULE is not set.
I expect that the $DJANGO_SETTINGS_MODULE would be None. It
is.
In: echo $DJANGO_SETTINGS_MODULE
Out:
Contents of .virtualenv/browsing/bin/postactivate
As documented in the django-pytest documents, one may set
$DJANGO_SETTINGS_MODULE by exporting it in the postactivate
script for virutalenv.
export DJANGO_SETTINGS_MODULE=automated_browsing.settings
At terminal cli:
As expected, $DJANGO_SETTING_MODULE is defined.
# activate the virtualenv
In: workon browsing
In: echo $DJANGO_SETTINGS_MODULE
Out: automated_browsing.settings
As expected, the settings module is set as shown when the server runs:
# cd into django project
In: cd ../my_django/automated_browsing
# see if server runs
In: python manage.py runserver
Out: # output
Validating models...
0 errors found
September 02, 2014 - 10:45:35
Django version 1.6.6, using settings 'automated_browsing.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
In: ^C
As NOT expected, py.test cannot find it.
In: echo $DJANGO_SETTINGS_MODULE
Out: automated_browsing.settings
In: py.test tests/test_browser.py
Out: …
_pytest.config.UsageError: Could not import settings
'automated_browsing.settings'
(Is it on sys.path? Is there an import error in the settings file?)
No module named automated_browsing.settings
installed packages
Django==1.6.6
EasyProcess==0.1.6
PyVirtualDisplay==0.1.5
South==1.0
argparse==1.2.1
ipython==2.2.0
lxml==3.3.6
psycopg2==2.5.4
py==1.4.23
pytest-django==2.6.2
pytest==2.6.1
selenium==2.42.1
test-pkg==0.0
wsgiref==0.1.2
Had the same problem today. In the project/settings/ you have to create init.py . Like this django recognize the folder.
In my case I just typed __inti__.py which was wrong. I corrected inti.py --> init.py and it works fine.
Hope it helps.
V.
Inspired by this answer, I have solved my problem.
According to the django-pytest documentation for managing the python path:
By default, pytest-django tries to find Django projects by
automatically looking for the project’s manage.py file and adding its
directory to the Python path.
I assumed that executing py.test tests/test_browser.py inside of the django project containing manage.py would handle the PYTHONPATH. This was an incorrect assumption in my situation.
I added the following to .virtualenv/$PROJECT/bin/postactivate:
export PYTHONPATH=$PYTHONPATH:$HOME/development/my_python/my_django/automated_browsing
And now py.test works as expected.

Django doesn't create translation .po files

I have my translation strings only in templates (stored in the project_dir/Templates), I tried running the $ django-admin.py createmessages -l ru both in the project root directory and in the app directories that use templates with trans. strings. It created folders locale/ru/LC_MESSAGES but the folders were empty. I tried to add the django.po files manually (with the syntax mentioned in the l10n docs). And ran the createmessages -a and compilemessages commands. It created the .mo files, but the translations didn't appear in the browser.
As I created the .po files manually I had no lines starting with #. What should I write there?
My template files are in different folder than the .py files for apps. Should I put some extra links to them?
did you try :
python manage.py makemessages -a
from project root and app ?
this should create a .po that you have to edit.
be sure to remove 'fuzzy' stuff everywhere.
then :
python manage.py compilemessages
You need to restart the server
For newer versions of Django (e.g. 3.2):
in the root directory create folder "locale"
run command django-admin makemessages -l ru
update your language files (located in the locale folder)
run django-admin compilemessages
Configure the LOCALE_PATHS in settings.py, otherwise you won't see the translations:
LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')]
LANGUAGE_CODE = 'ru'
To fix empty po files:
Make sure you did the needed changes for the templates as mentioned in the documentation. Please make sure that you are checking the correct documentation version for your project.
You can add a locale directory in your templates directory and add its path to the LOCALE_PATHS list. (Optional, but helpful to make sure that the template directory is included in step 4)
Go to the project_dir (you should run the next command in a parent directory of the files to be translated)
Run the command django-admin makemessages -l ru

Categories

Resources