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

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.

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.

Django command without Django application?

Is there any way I can add a custom Django command which doesn't need an underlying application or project to work (pretty much like startapp or startproject)?
$ django-admin.py startveryniceproject projectname
One way I can imagine this could be accomplished would be to copy a startveryniceproject.py into django.core.management.commands, but does that really sound "clean"?

Django basic project with common functionality included

Does someone know any basic open source Django project with follow common applications installed:
Native Registration with templates.
Open auth registration.
Basic templates created.
Twitter Bootstrap included
Main page with menu app.
Maybe some other common things.
Something that you can start with, and jump to your special functionality and design right away.
The only project that I'm aware of is django-skel
But it seemed does not include all the really common things.
Pinax looks like drooped support from django-1.3 version.
django-skel is meant as sane configuration for Django projects which clears up a lot of configuration you need to do to when you start a project.
Now it looks like what you are looking for falls closer to the Pinax ball park.
That being said this wont take you long to setup yourself and you wont have to deal with the legacy of something like pinax albiet it gets you started really quick. They have a bunch of reusable apps that you could string together to get your desire result.
Sorry for disturbing without proper research, but here is something I just found and it seems totally awesome:
$ mkvirtualenv mysite
$ pip install Django==1.4.1
$ django-admin.py startproject --template=https://github.com/pinax/pinax-project-account/zipball/master mysite
$ cd mysite
$ pip install -r requirements.txt
$ python manage.py syncdb && python manage.py runserver

How does django register management commands

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.

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.