Django makemessages doesn't see locales in locale_paths - python

I have custom locale path in my settings
PROJECT_ROOT = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
LOCALE_PATHS = (
os.path.join(PROJECT_ROOT,'templates','v1','locale'),
)
but when i'm trying to create .po files, i've get error:
$ python manage.py makemessages --locale=ru
CommandError: This script should be run from the Django Git tree or your project or app tree. If you did indeed run it from the Git checkout or your project or application, maybe you are just missing the conf/locale (in the django tree) or locale (for project and application) directory? It is not created automatically, you have to create it by hand if you want to enable i18n for your project or application.
Why django doesn't want to use LOCALE_PATHS?
Absolute paths are:
PROJECT_ROOT = '/home/creotiv/ENVS/project_env/project/project'
LOCALE_PATHS = ('/home/creotiv/ENVS/project_env/project/project/templates/v1/locale')
PS: Also i added translation to main app and django doesnt see it either.

First things first; You need to give correct path of the manage.py to the python.
$ python /path/to/the/manage.py makemessages --locale=ru
Or go to directory which is included manage.py. After that run that command you've wrote in your question.
$ python manage.py makemessages --locale=ru
Next; Did you create ./templates/v1/locale directories under the project home?
According to error you may check existence of the ./templates/v1/locale' folder.
Also; You may need to append LANGUAGES setting in to project settings.py file:
LANGUAGES = (
('ru', _('Russian')),
('en', _('English')), # (Optional)
)
Appendix:
If you are developing under virtual environment, do not forget to enable virtual environment first.

Maybe you have to go inside the app or project directory. Try this:
$ ls # should return also the manage.py
$ cd myproject
$ ls # should return also the settings.py
$ python ../manage.py makemessages --locale=ru

Just updated to django 1.7 and problem gone.

Related

Manage.py unknown command

I am a student and my profesor needs me to install Django on PyCharm.
I made a big folder called PyCharmProjects and it includes like everything I have done in Python.
The problem is that I made a new folder inside this PyCharmProjects called Elementar, and I need to have the Django folders in there but it's not downloading.
I type in the PyCharm terminal django-admin manage.py startproject taskmanager1 (this is how my profesor needs me to name it)
After I run the code it says:
No Django settings specified.
Unknown command: 'manage.py'
Type 'django-admin help' for usage.
I also tried to install it through the MacOS terminal but I don't even have acces the folder named Elementar (cd: no such file or directory: Elementar) although it is created and it is seen in the PyCharm.
Manage.py its python file after you start your project, you cant call this file until this command:
django-admin startproject mysite
Then run:
python manage.py runserver
And if you want apps in your project run:
python manage.py startapp my_app
First of all, you can't create a project using manage.py because the manage.py file doesn't exist yet. It will be created automatically in the folder taskmanager1 if you run the command below.
You can create a project with the command
django-admin startproject taskmanager1
After that you can change the directory to the taskmanager1 folder with the cd taskmanager/ command.
When you changed the directory you can use the python manage.py commando for example if you want to run your migrations or creating an app.
python manage.py migrate

cannot open manage.py after installing django

I have a problem in setting up django.
My situation: I have Anaconda Python 2.7 in my Windows 8 computer. On the Anaconda command prompt window, I type: pip install django. This is successful.
Then I create a folder named "newproject". On the command prompt I went to the folder "newproject". Then django-admin.py startproject newproject. This is successful.
Then I run python manage.py runserver. It tells me
"...can't open file 'manage.py': [Errno 2] No such file or directory"
I checked out udemy django installation guide and other guides on the net. I have even set up a virtual environment. But the main problem is always: can't open file 'manage.py'
You're not in the proper directory...In the case you described, you should have:
mkdir newproject (not sure why you're doing this...but we'll continue with it)
cd newproject
django-admin.py startproject newproject
cd newproject ← A key missing part from what you did. You need to change into the directory where manage.py resides. Verify it by using ls at the command prompt after switching into the directory.
python manage.py runserver
Use ls often, if you need, to double check where you are in the directory tree.
You are not in the correct directory. You need to do cd newproject and the execute your runserver command.
For me it was because the manage.py didn't get created and the problem was that:
In windows instead of typing "django-admin.py" just type "django-admin" and it will create the manage.py file
you need to change your directory to the directory of the project.
open your environment by typing activate environment_name
change the directory to the folder in which you want to create the
project by cd Folder_Name
then create your project by django-admin startproject
Project_Name
then change the directory to your project folder
if you also want to create an app then typepython manage.py
startapp App_name
to confirm if it works type python manage.py runserver, then
you should get a link in the terminal. copy and paste that link in
the browser and you should see a successful page.
The reason of this problem is because you're in the first project directory "parent directory", and you have to go to your project "newproject" that has the manage.py file.
The way to this is simple:
cd "name of your project", E.g. cd newproject
python manage.py runserver
You need to include a . after you create the project.
Eg: django-admin.py startproject newproject .
This will allow you to run python manage.py runserver from where you are.
That can happen because manage.py doesn't have permissions.
Check the existence of manage.py throughout newproject, If exist you can fix it with:
$cd newproject
$chmod +x manage.py
Finally, try to execute python manage.py runserver
I was having the same problem, when Windows 10 changed credentials, Docker lost the access to drives. The fix for this issue, I reset the credentials in docker-desktop > shared Drives > reset credentials at bottom.
can't open file 'manage.py': [Errno 2] No such file or directory"
here is the solution
step 1: command -> django-admin startproject project_name
step 2: command -> cd project_name
step 3: command -> python manage.py startapp app_name
try it may fix your problem
You should navigate to the inner directory where your manage.py resides. For example:
If you have created projectname as
Django-admin startproject loginapp
Step 1: Go to loginapp
Step 2: Click on it
Step 3: You will find another loginapp and manage.py folder inside that
Step 4: Use this rootpath commandprompt
Step 5: Enter py manage.py runserver
Now it will work
I ran into the same error seller. But this is what I discovered
1: open your terminal in your project environment
2: install Django using: pip install django (if you are on windows)
3: then create your Django project using : django-admin startproject newproject . (django-admin start-project followed by project name give a space and add period (.) )
So it goes like this
django-admin startproject new project .
The period/fullstop (.) At the end tells djando the area in which the file should be created which is inside the project file its self you are working on. After this is done you are free to go. You can now run your server and enjoy
Run server using: python manage.py runserver
Make sure to put a period at the end of your command e.g
Django-admin startproject new project .
The period at the end (.) will solve the problem
There could be invalid interpreter selected .Try selecting the proper interpreter

Running Django Custom Management Command - Path Issues

I don't understand why this doesn't find the command:
khandelwal#simba:/opt/code/cdcschool$ python schoolcommand/manage.py createcampaign
Unknown command: 'createcampaign'
Type 'manage.py help' for usage.
When this works fine:
khandelwal#simba:/opt/code/cdcschool$ cd schoolcommand/
khandelwal#simba:/opt/code/cdcschool/schoolcommand$ python manage.py createcampaign
Error: Provide: <type start_date end_date>
This is where my command is located:
$ ls schoolcommand/management/
commands __init__.py __init__.pyc
$ ls schoolcommand/management/commands/
campaignmanager.py createcampaign.py __init__.py
campaignmanager.pyc createcampaign.pyc __init__.pyc
How do I fix it so that I can do:
khandelwal#simba:/opt/code/cdcschool$ python schoolcommand/manage.py createcampaign
Here are the values of my PYTHONPATH, DJANGO_SETTINGS_MODULE and the one place my settings.py is located.
khandelwal#simba:/opt/code/cdcschool$ ls
Procfile README requirements.txt schoolcommand
khandelwal#simba:/opt/code/cdcschool$ echo $PYTHONPATH
khandelwal#simba:/opt/code/cdcschool$ echo $DJANGO_SETTINGS_MODULE
khandelwal#simba:/opt/code/cdcschool$ find . -name settings.py
./schoolcommand/settings.py
khandelwal#simba:/opt/code/cdcschool$
When you use Django, there are two important rules.
First.
You have a settings.py file which must be used by the web server and all the manage.py commands. All of them.
The default place to look for the settings.py file is the current working directory. You can change this with the PYTHONPATH and the DJANGO_SETTINGS_MODULE environment variable.
The manage.py is created for you in the same directory as the settings.py.
You can use django-admin.py --settings=some.module if you don't want to use manage.py.
Second.
The manage.py commands do not have any "path" to them. They're all just one-word commands, no matter where they happen to live in your application tree.
You never do this: python schoolcommand/manage.py createcampaign unless (somehow) your settings.py is not in the same directory as your manage.py.
You normally do this:
cd /path/to/your/settings
python manage.py createcampaign
If your settings is in code/schoolcommand that means that your web site and all your commands will operate in that directory.

Django App Install Script - How to add app to INSTALLED_APPS setting?

I've written a Django app, and now I want to make it easy to deploy on multiple servers.
The basic installation is:
Copy the app folder into the Django project folder
Add it to INSTALLED_APPS in settings.py
Run ./manage.py collectstatic
This particular app doesn't need to use the DB, but if it did, I'd use south and run ./manage.py migrate, but that's another story.
The part I'm having trouble with is #2. I don't want to have to manually edit this file every time. What's the easiest/most robust way to update that?
I was thinking I could use the inspect module to find the variable and then somehow append it, but I'm not having any luck. inspect.getsourcelines won't find variables.
You can modify your settings.py using bash.
#set $SETTINGS_FILE variable to full path of the your django project settings.py file
SETTINGS_FILE="/path/to/your/django/project/settings.py"
# checks that app $1 is in the django project settings file
is_app_in_django_settings() {
# checking that django project settings file exists
if [ ! -f $SETTINGS_FILE ]; then
echo "Error: The django project settings file '$SETTINGS_FILE' does not exist"
exit 1
fi
cat $SETTINGS_FILE | grep -Pzo "INSTALLED_APPS\s?=\s?\[[\s\w\.,']*$1[\s\w\.,']*\]\n?" > /dev/null 2>&1
# now $?=0 if app is in settings file
# $? not 0 otherwise
}
# adds app $1 to the django project settings
add_app2django_settings() {
is_app_in_django_settings $1
if [ $? -ne 0 ]; then
echo "Info. The app '$1' is not in the django project settings file '$SETTINGS_FILE'. Adding."
sed -i -e '1h;2,$H;$!d;g' -re "s/(INSTALLED_APPS\s?=\s?\[[\n '._a-zA-Z,]*)/\1 '$1',\n/g" $SETTINGS_FILE
# checking that app $1 successfully added to django project settings file
is_app_in_django_settings $1
if [ $? -ne 0 ]; then
echo "Error. Could not add the app '$1' to the django project settings file '$SETTINGS_FILE'. Add it manually, then run this script again."
exit 1
else
echo "Info. The app '$1' was successfully added to the django settings file '$SETTINGS_FILE'."
fi
else
echo "Info. The app '$1' is already in the django project settings file '$SETTINGS_FILE'"
fi
}
Use:
add_app2django_settings "my_app"
Here are my reasons why I think this would be wrong:
it is extra code complexity without any big need, adding one line to settings every time is not that bad, especially if you are doing step #1 and #3.
it will become not explicit what apps your project is using. When another developer will work on your project, he might not know that your app is installed.
you should do step #1 and step #2 on code versioning system, test the whole system and then commit the changes and just then deploy it.
I think you have something wrong (from my point of view) in your develop/deploy process if you are looking for such an "optimization". I think it is much easier and better to use INSTALLED_APPS.
If you are building something for public use and you want to make it as easy as possible to add modules then it would be nice. In this case I would recommend to package project and it's apps as python eggs and make use of entry points. Then you could deploy an app into project like this:
pip install my-app-name
Without even step #1 and #3! Step #1 will be done by pip, and step #2 and #3 will be done by setup hooks defined in your project.
Paste script is a good example of entry-points utilization:
# Install paste script:
pip install pastescript
# install django templates for pastescript:
pip install fez.djangoskel
# now paste script knows about fez.djangoskel because of entry-points
# start a new django project from fez's templates:
paste create -t django_buildout
Here is a portion of setup.py from fez.djangoskel package:
...
entry_points="""
[paste.paster_create_template]
django_buildout=fez.djangoskel.pastertemplates:DjangoBuildoutTemplate
django_app=fez.djangoskel.pastertemplates:DjangoAppTemplate
...
zc.buildout is another great tool which might make your deployments much easier. Python eggs plays very nice with buildout.

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