Django, error with custom admin commands when executing with absolute path - django

I have a custom admin command named temperature.py which is under /home/user/project/monitor/management/commands. If I change directory to /home/user/ and execute:
user#localhost:~/project$ ./manage.py temperature
It runs ok, and its listed in the available commands. But if I try running it with the absolute path:
user#localhost:/$ /home/user/project/manage.py temperature
It says the command does not exist, it does not show up in the available commands either. I have django 1.2.1 with python 2.6.5 in ubuntu 10.04. Could this be a problem with django? is it the python version? Thanks in advance

Found the reason, it seems that django is looking for the settings under the main directory, if it fails to find one, it will use the defaults. You can change your python path or use this in your manage.py file

Related

Not able to activate debug in VS-code for Django project (Django in virtual environment)

I have an issue running the Debug environment for Django application in VS-code:
my python is not in the virtual environment,
while my django is.
Therefore, the solution I see everywhere with adding to the launch.json settings the line "pythonPath": "${workspaceRoot}/.venv/bin/python2.7",
does not fit, and I end up having the following error:
Exception has occurred: ImportError
Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable?
Did you forget to activate a virtual environment?
When working on the application, I launch the setting environement using the following command:
.\venv\Scripts\activate.ps1
Can anyone help me out to declare my virtual environment the proper way in the launch.json file?
if you've created a virtual env lets say with python -m venv my_env, you should "select Interpreter" and define a path like this:
./my_env/bin/python
make sure you choose correct version (python2/3) and
usually there's no need to make launch.json manually.
Based on the suggestion of https://stackoverflow.com/users/13877794/omid, I was able to find the solution:
I opened command palette, chose Python: select Interpreter, and then after selecting the workspace I was interested in, I typed in .\venv\Scripts\python.exe which works fine for me.

Pyinstaller is unable to import custom commands of Django 2.1.5

I am using PyInstaller for creating build for my project on ubuntu machine.
I have written one custom commands createsappsuperuser in django to create sime special users. To create the executable file, I am running my manage.spec file and the executable is getting created successfully. The location of that executable is in dist folder.
After that I am running ./dist/manage createsappsuperuser which gives error like -
Unknown command: 'createsappsuperuser'. Did you mean createsuperuser?
Type 'manage help' for usage.
To solve the above issue, I tried adding my cusom command in the dict
in Pyinstaller/loader/rthooks/pyi_rth_django.py file as because pyi_rth_django.py always return some static set of commands.
I have added it like 'createsappsuperuser': 'idmgmt' in the dictionary. Here, idmgmt is my app name.
But the above solutions was valid for upto Django 1.8, and even the file "pyi_rth_django.py" says that it is tested with Django 1.8.
Then how should i do it in Django 2.1.5 ?
Thanks In Advance

In Django's shell, how can I load ipython magic functions automatically?

I want to make the following ipython commands permanent when using django shell:
%load_ext autoreload
%autoreload 2
Unfortunately, Django doesn't seem to use my global ipython config, so putting them in my default_profile doesn't seem to work. Is there any way to have these executed automatically when running django shell?
You can use the django extentions package, which contains a shell_plus command. This command autoloads the models, but you also can use the --notebook attribute. There you can add the autoload parameter: --ext django_extensions.management.notebook_extension.
See here for more info.

Is there a way to add custom django-admin.py commands that work outside of projects?

I'm trying to write a custom command that works outside of Django projects. I was thinking I could follow the coding patterns of Django's own such commands (e.g., startproject), include my command in an app and install it.
Alas, it seems django cannot see this command, as perhaps it doesn't scan site-packages for custom commands.
Is there a way to make this work or am I sadly correct?
UPDATE: I should note that the goal I was trying to accomplish (writing a command that starts projects based on custom templates) is supported in the coming 1.4 release of Django: https://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-startproject (see the --template option).
Based on this code from django.core.management, it does appear that django only searches for project-less commands in its own packages, and will then only find command by scanning INSTALLED_APPS, which means a project is required.
You can use a custom manage.py.
You do need a project. A project is, although, nothing more than a python package with a settings.py (and maybe a urls.py file)
So you could just create a project, with whatever commands you want, and in your setup script include a binary script that is nothing more than a manage.py in disguise.
I use it to have a manage.py in the bin path of a virtualenv, but you can call it something else and have that "django" project installed in your system python.
I don't quite understand from your post, for what purpose do You want to write such command using Django's manage.py. But suppose you want (as I was) to run some script, that works with Django models, for example. You cannot run such script without setting Django environment.
I do the following:
put my code in script.py
manage.py shell
execfile('script.py')
Maybe, this helps.

Django management task won't work on CentOS in crontab or outside project directory

On my local machine (Mac OSX 10.6) I wrote a django custom admin command which works great. I can use it both within and outside my project directory just fine. For some reason on my CentOS 5.6 server, it won't work from outside the project directory. This is really annoying since using this custom admin command in a cron job requires it to run from the home directory.
in short:
When I run "python ./manage.py scrape" or "python manage.py scrape", everything is fine.
When I run "python /home/[username]/webapps/myproject/manage.py scrape" or "python myproject/manage.py scrape", I get the following error:
unknown command: 'scrape'
Type 'manage.py help' for usage.
On CentOS, when I run manage.py help inside the project directory, scrape shows up as a command; but if I run it outside the project directory, scrape does not appear as a valid command. On OS-X scrape appears as a valid command regardless of where I run manage.py help from.
Any idea how I can fix this?
I know CentOS ships with Python 2.4, so is your code running on 2.4 or are you using a contained environment, this is usually fixed by adding your PYTHONPATH correctly
import sys
print sys.path
verify such for starters
This should get you up and running: http://djangosnippets.org/snippets/374/