How does django register management commands - django

I'm having a weird issue with something in a deployed django app (long story).
Something that might help me is to know:
How django goes about detecting and maintaining a list of active management commands?

Some things you should consider if you have troubles executing a management command:
The app containing the command has to be in settings.INSTALLED_APPS.
To be recognized as an app the package has to contain a models.py (although it can be empty).
All packages need to have an __init__.py file (your app's directory as well as the management and command folder).
Sometimes Django seems to be choking on something like an ImportError that doesn't get displayed properly - so it might help to open a manage.py shell and try something like import MyCommand from myapp.management.commands.mycommand.

It looks for any module under management.commands inside installed applications. See https://docs.djangoproject.com/en/dev/howto/custom-management-commands/ for more details.
The code looks for Python modules in management/commands direction in each installed app using pkgutil.iter_modules.

Related

django site urls whose are not under site-packages

Lets assume you have a django website svn directory which is not under site-packages.
Whenever i run:
mysyper_dir/whatever_module/manage.py runserver 0.0.0.0:8888
and then connect, I realize that my request are still handled by the python files in:
....../site-packages/whatever_module/
while I can see the prints of
mysyper_dir/whatever_module/setting.py
from my server console.
is there a way to tell django that, every "non-framework" files it will ever need are in the "mysyper_dir/whatever_module" directory ?
Neither of these are Django specific (Python actually already handles this) but there's a couple of things you could do. You could set the environment variable PYTHONPATH, or you could add directories to sys.path. You can find more about where modules are located here.
If you are looking to have things just apply to Django, then adding to sys.path might be your best bet. You could try something weird like modifying manage.py and adding command line arguments after the #!/usr/bin/env python but that's uncharted territory for me.

Installing Pinax, where is my deployment folder?

I'm trying to set up Pinax and I'm new to everything involved (Django, Pinax, webservers, etc). I'm following http://pinax.readthedocs.org/en/latest/gettingstarted.html
When I generate a project using:
(mysite-env)$ pinax-admin setup_project -b basic mysite
The directory structure I get is:
apps __init__.py manage.py settings.pyc urls.py
dev.db __init__.pyc requirements static urls.pyc
fixtures locale settings.py templates wsgi.py
Which as far as I can tell is missing the deployment folder (when you compare to the directory structure shown here : http://pinax.readthedocs.org/en/latest/starterprojects.html). It doesn't seem to be effecting anything yet, but it makes me nervous. What is going on and is the fact I'm missing the deployment folder going to cause problems in the future?
I'm running Ubuntu and using python 2.7. I had the same behaviour with Windows 7, python 2.6
Thanks!
The new Django versions have made the old pinax pretty much useless. Now Django supports project templates and Pinax is separated into several smaller projects regarding starter projects (such as pinax-project-account) and apps (such as django-user-account).
The current way to use pinax is to choose a starter project, and then running something like:
$ django-admin.py startproject --template=https://github.com/pinax/pinax-project-account/zipball/master <project_name>
and then install requirements:
$ pip install -r requirements.txt
This will create a new Django project using the starter-project as a template, which already includes a few apps (like django-user-account) and templates (with bootstrap!). The project is ready to run, and already includes a bunch of functionality (like user registration, login and management).
Also, Django has changed the project directory structure a bit, so now it doesn't really look like that anymore.

Deploying multiple projects on Heroku with Django

I would like to deploy two separate Django applications to Heroku. Two applications, with two separate domain names, that are logically different from each other. I set up a venv that contain all the Python/Django stuff. Now, I could create another application that duplicates all the Python/Django stuff in another project. But, is there a way to use the same venv?
My file structure currently looks like this
django
-.git
-projectname_1
-venv
.gitignore
requirements.txt
When I tried to add projectname_2 under django I got an error saying Django app must be in a package subdirectory
Is there a correct way to add a second application using the same venv?
This error occurs when your project doesn't conform to Heroku's specs for a Django project.
Specifically, that particular error occurs when Heroku did not find a settings file at ~/your_app_name/settings.py and therefore assumed it's a non-Django Python app. But then it did find settings.py and manage.py at your project root (~/).
The specific Heroku source code that throws this error is:
https://github.com/heroku/heroku-buildpack-python/blob/master/bin/compile
Your directory should look something like this:
~/.gitignore
~/Procfile
~/requirements.txt
~/your_app_name/
~/your_app_name/manage.py
~/your_app_name/settings.py
~/your_app_name/etc...
Your best bet really is to use two separate Heroku apps. Heroku makes some assumptions about what type of app you are deploying and doesn't necessarily account for multiple apps.
Also, it's probably best to not check in your virtualenv. Just make sure all your dependencies are defined in requirements.txt and Heroku will install them automatically inside a new virtualenv.

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 custom commands not showing up on Heroku

I’m having a problem when using django custom commands on Heroku.
On my local machine, the custom command appears in help if I run ./manage.py help and running ./manage.py deletedphotos runs it too.
All the init.py files are there and the settings are also correct, i.e. there are not major config differences between my local and Heroku instances.
Now, when I put it on Heroku, it does not show up. All my other non-default commands are there: ping_google that comes from installing sitemap.xml support and commands for south migrations. But for some reason, my self written commands do not show up.
I’ve also sent a support request to Heroku, but haven’t heard back from them in a few days, so I thought I’d post here as well, maybe someone has had any similar problems.
The deletedphotos.py file contents are pretty much like this if that matters anything:
from django.core.management.base import BaseCommand, CommandError
from foo.app.models import *
class Command(BaseCommand):
help = 'Delete photos from S3'
def handle(self, *args, **options):
deleted_photos = Photo.objects.filter(deleted=True).exclude(large='', small='', thumb='')
self.stdout.write('Found %s photos\n' % str(len(deleted_photos)))
I’ve tried checking all the correct python paths etc, but not 100% if I’m not missing something obvious.
I was actually able to find a solution. The INSTALLED_APPS had my local app referenced, but for some reason it was not working as intended.
My app is in: /name/appname/ and having 'name.appname' in INSTALLED_APPS was working fine in local setup.
Yet, on Heroku, I had to change the reference to just 'appname' in INSTALLED_APPS and all started working magically.
Your home directory needs to be on your Python path. A quick and unobtrusive way to accomplish that is to add it to the PYTHONPATH environment variable (which is generally /app on the Heroku Cedar stack).
Add it via the heroku config command:
$ heroku config:add PYTHONPATH=/app
That should do it! For more details:
http://tomatohater.com/2012/01/17/custom-django-management-commands-on-heroku/
I had this problem too, found the answer here: Django Management Command ImportError
I was missing an __init__.py file in my management folder. After adding it everything worked fine.
Example:
qsl/
__init__.py
models.py
management/
__init__.py
commands/
__init__.py
news.py
jobs/
__init__.py
news.py
tests.py
views.py