Why can't I use 'django-admin.py makemessages -l cn' - python

print :
D:\zjm_code\register2>python D:\Python25\Lib\site-packages\django\bin\django-adm
in.py makemessages -l cn
Error: This script should be run from the Django SVN tree or your project or app
tree. If you did indeed run it from the SVN checkout or your project or applica
tion, 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 ha
ve to create it by hand if you want to enable i18n for your project or applicati
on.
2.i made a locale directory ,and
D:\zjm_code\register2>python D:\Python25\Lib\site-packages\django\bin\django-adm
in.py makemessages -l cn
processing language cn
Error: errors happened while running xgettext on __init__.py
'xgettext' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
D:\Python25\lib\site-packages\django\core\management\base.py:234: RuntimeWarning
: tp_compare didn't return -1 or -2 for exception
sys.exit(1)
3.
ok
http://hi.baidu.com/zjm1126/blog/item/f28e09deced15353ccbf1a82.html

is register2 your project or app tree?
did you make directory register2\\locale?

Related

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

Django makemessages doesn't see locales in locale_paths

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.

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.

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.

Categories

Resources